U2U Blog

for developers and other creative minds

Deploying a Silverlight application as an Embedded Resource

While my world stopped turning 10 days ago, technology has not. The coming weeks I will post things I stopped working on because my work for Patrick took too much of my (free) time. By keeping up the work I hope to honor him. Thanks for the many words of support and the condolences.

When integrating Silverlight applications with SharePoint custom development, you have different possibilities where to deploy your silverlight application:

- To the ClientBin folder of your IIS web application: If you deploy in this location, it means that the Silverlight application can be picked up by any SharePoint code that runs in the site collections and sites hosted on the IIS Web Application.

- To the 12\Template\Layouts or in the 12\Template\ControlTemplates folder: deploying here means that you can include all of the deployment steps in your SharePoint Solution.

- To a document library: you can create one central document library within your site collection (or if you want a more narrow scope, for your site) where to drop the XAP files.

I refer to the post of Patrick Tisseghem for more details, advantages and disadvantages of each possibility.

In this post I will show you how you can deploy your Silverlight application as an embedded resource of your web part. In that case the Silverlight application will only be available to that web part.

The first step is to build your silverlight application. I used the Hello Silverlight 2.0 application from the Silverlight BluePrint for SharePoint.

Create your web part

Then create your web part. I used the SharePoint WebPart template for Visual Studio 2008.

Add a reference to the following DLLs:

- System.Web.Silverlight.dll (version 2.0.5.0)

- System.Web.Extensions.dll (version 3.5.0.0)

Add your silverlight application (.xap) to your Visual Studio project. I created a Resources directory for it. Set the properties of the xap to Embedded Resource. Don't forget this because otherwise the Silverlight application will not be compiled as embedded resource.

image 

Open the Web Part class. Before the namespace declaration add the metadata attribute that enables an embedded resource in an assembly:

[assembly: WebResource("EmbeddedSilverlightWebPart.Resources.SL.XAML.HelloSilverlight20.xap", "application/x-silverlight-app")]

Check the correct namespace with Reflector.

Add a class level variable for the Silverlight control. This control resides in the System.Web.Silverlight namespace.

Silverlight silverlightControl;

Override the OnLoad event to check whether a Script Manager already lives on the Web Part page.

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    // Script manager instance may appear only once on a page
    ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
    if (scriptManager == null)
    {
        scriptManager = new ScriptManager();
        this.Controls.AddAt(0, scriptManager);
    }
}

Instantiate the Silverlight control in the CreateChildControls method and add it to the Controls collection. The Source property of the silverlight control need to be set to the location of the embedded resource. This location can be detected by using the  this.Page.ClientScript.GetWebResourceUrl method:

protected override void CreateChildControls()
{
    base.CreateChildControls();

    this.Width = new Unit("400");
    this.Height = new Unit("200");

    string xapUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "EmbeddedSilverlightWebPart.Resources.SL.XAML.HelloSilverlight20.xap");
    silverlightControl = new System.Web.UI.SilverlightControls.Silverlight()
    {
        ID = "EmbeddedSlCtl",
        MinimumVersion = "2.0.30523",
        Source = xapUrl,
        Width = new System.Web.UI.WebControls.Unit(400),
        Height = new System.Web.UI.WebControls.Unit(200)
    };
    this.Controls.Add(silverlightControl);
}

Override the RenderContents method:

protected override void RenderContents(HtmlTextWriter writer)
{
    EnsureChildControls();
    if (silverlightControl != null)
        silverlightControl.RenderControl(writer);
}

That's it. Build your code.

Deploy the web part

Make the necessary changes to the .webpart file and the .xml file if you want to. Patrick learned me to always add the web part to a significant group in the Web Part Gallery:

<Elements Id="1eba021c-8a0f-4bf2-8f2e-1c9d8f36bf7f" xmlns="http://schemas.microsoft.com/sharepoint/" >
  <Module Name="WebParts" List="113" Url="_catalogs/wp">
    <File Path="EmbeddedSilverlight.webpart" Url="EmbeddedSilverlight.webpart" Type="GhostableInLibrary">
      <Property Name="Group" Value="Silverlight" />
    </File>
  </Module>
</Elements>

Deploy the web part to your SharePoint site. If you use the Visual Studio 2008 extensions for SharePoint, you only have to enter the URL of your SharePoint site in the project properties and press F5.

Don't forget to configure your SharePoint sites for enabling Silverlight if not yet done.

Add the Web Part to a Web Part page

image

You can download the sample web part here.

Thanks to Stefaan Rillaert for pointing me in the right direction.

Comments (9) -

  • 9/14/2008 11:21:02 PM |

    And thanks for the reference Karine. Smile  I hope you keep on doing great work.  Stefaan

  • 11/2/2008 11:37:18 PM |

    Hi,  I use SL2, and your code is not work

  • 11/3/2008 3:37:27 AM |

    I'm very sure this code works because I already used it on many occasions. You can leave me a message on where you encounter problems and I will try to help you out.

  • 11/3/2008 11:07:46 PM |

    I'm sorry. My mistake. It worked. Thank you very much.

  • Coogi shorts Jeans

    6/15/2010 3:01:33 AM |

    Great post. Thanks so much. Yes, sometimes it seems we’re typing to the cyber-breezes…. then, someone will leave a comment that really connects you to the outside world and you know you are in a community.

  • Air Jordan 16

    6/15/2010 8:34:24 AM |

    It is great to see how your success has developed and the long road you’ve taken. My sincerest wish is to be in a similar situation, one day. As Keisha already said (and I believe this, too) … you, your blog and your thoughts have been very inspiring to many people. Thanks so much!

  • FB Ads Guide review

    11/12/2010 8:55:05 PM |

    Great stuff.I'd like to recommend checking out a lot around the idea of french fries. What are your thoughts?

  • Bianca Doyel

    11/16/2010 10:45:57 PM |

    Awesome post, hey I found this post while googling for lyrics. Thanks for sharing I’ll email my friends about this too.

  • Natashia Moschetto

    11/18/2010 4:56:14 AM |

    I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful.

  • dry loop dsl providers in chicago 2

    11/26/2010 3:00:19 PM |

    Nobody in life gets exactly what they thought they were going to get. But if you work really hard and you’re kind, amazing things will happen.

  • buy phentermine no prescription

    11/28/2010 10:39:23 PM |

    Im impressed.  I dont think Ive met anyone who knows as much about this subject as you do.  Youre truly well informed and very intelligent.  You wrote something that people could understand and made the subject intriguing for everyone.  Really, great blog youve got here.

  • buy phentermine

    12/1/2010 1:25:33 AM |

    Is this WP theme free or premium? nice blog btw!

  • Clemencia Galanti

    12/5/2010 9:06:45 AM |

    This blog appears to recieve a good ammount of visitors. How do you advertise it? It gives a nice unique twist on things. I guess having something useful or substantial to say is the most important thing.

  • buy phentermine fast

    12/6/2010 8:03:21 AM |

    love this site – it’s a great site – may i suggest you get an rss feed.

  • stojaki reklamowe

    12/8/2010 6:38:01 PM |

    I was very pleased to find this site.I wished to regards for this fantastic read!! I definitely enjoying every little bit of it and I have you saved as a favorite to check out new stuff you article.

  • Potykacze

    12/8/2010 6:38:11 PM |

    I like this  web site so much,  saved to my bookmarks .

  • Buy Facebook Fans

    12/9/2010 3:59:13 AM |

    Completely agree. Discovered this nice website whilst looking for a good one to remark on. Nicely performed!

  • Car Battery Charger For Sale

    12/10/2010 2:54:10 PM |

    Nobody in life gets exactly what they thought they were going to get. But if you work really hard and you’re kind, amazing things will happen.

  • medium haircuts for 2013

    12/21/2010 5:44:50 PM |

    Every great advance in natural knowledge has involved the absolute rejection of authority.

  • Cornell Abundiz

    12/28/2010 5:53:17 PM |

    I wanna wish you the best and would like to thank you for the site.

  • Drug rehab

    12/28/2010 10:48:30 PM |

    Alcohol abuse has the same definition but is manifested by one (or more) of the following behaviors occurring within the same 12-month period:   <a href="http://bit.ly/fHdiTH">; Alcoholism </a>

  • Bianca Charriez

    12/29/2010 7:28:08 PM |

    Hi I am getting a technical issue visiting your site, I’m receiving 403 messages extremely frequently, I'm not very clear about why but once I refresh the webpage it comes fine.

  • Sherrell Groom

    12/29/2010 8:27:11 PM |

    Hello there I'm experiencing a technical issue browsing your web site, I am getting 502 pages quite often, I am not very sure as to the reasons but when I refresh the page it returns right.

  • Octavia Kinter

    12/30/2010 12:02:53 AM |

    Hey there I am experiencing a problems with visiting your site, I’m receiving 501 pages extremely regularly, I am not way too sure why but once I reload the webpage it returns right.

  • hello kitty jewelry game

    12/30/2010 1:32:08 PM |

    Can I have a bit of advice?  I do believe you've got something great above.  Yet what if you also offered a couple references to a page which supports what  you were you mentioned?  Or maybe you could offer us some extra information to look at, something that might associate what exactly you are mentioning, sth more concrete?

  • hello kitty games

    12/30/2010 4:32:58 PM |

    May i make a word of advice?  There's no doubt that youve got sth good above.  Yet suppose you also included a few links to a page that backs up what  you're you said?  Or maybe you may provide us with something to check out, something that might connect what exactly you were saying, sth  tangible?

  • MCAT classes

    12/31/2010 12:44:16 PM |

    Hi I am having a technical issue reading your site, I am given 505 messages really regularly, I am not far too clear about why however if I refresh the web page it comes alright.

  • MCAT practice tests

    12/31/2010 4:21:56 PM |

    Hi there I'm keeping getting a technical obstacle visiting your site, I am getting 502 errors extremely often, I'm not way too sure as to the reasons however if I reload the page it returns fine.

  • Sally Melendez

    12/31/2010 8:34:49 PM |

    Please, keep up the greet work and continue to post topics like this. I am old fan of your site.

  • artyku&amp;#322;y reklamowe

    1/2/2011 7:49:27 PM |

    Thanks a lot for your interesting post. I have been looking for such post for a very long time. Not all your content is fully easy to understand to me, even though it is definitely interesting and worth reading.

  • Irene Mccauley

    1/3/2011 7:32:00 PM |

    There is obviously a bunch to identify about this.  I assume you made various good points in features also.

  • repertuar teatr&amp;#243;w

    1/4/2011 5:20:06 PM |

    it is a really good point of view. I usually meet people who rather say what they suppose others want to hear. Good and well written! I will come back to your site for sure!

  • prawo jazdy wroc&amp;#322;aw

    1/8/2011 12:22:25 AM |

    It is truely very good post, but I do not see everything completely clear, especially for someone not involved in that topic. Anyway very interesting to me.

  • Antonio Govostes

    1/19/2011 5:30:49 PM |

    I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.

  • Torty

    2/1/2011 3:36:32 PM |

    I found your post very interesting.

  • zespol na wesele

    2/4/2011 12:03:02 PM |

    Za kilka miesięcy wychodzę za mąż, ale cię cieszę!

  • Rubin Bowersock

    2/4/2011 8:17:22 PM |

    The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  • Gotowe Prace Magisterskie

    2/4/2011 8:50:49 PM |

    The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention. Look at my www  <a href=”http://www.pisanieprac.pl”>prace magisterskie</a>. See You...

  • Prace Magisterskie

    2/4/2011 9:14:49 PM |

    Youre so cool! I dont suppose Ive read anything like this before. So nice to find somebody with some original thoughts on this subject. realy thank you for starting this up. this website is something that is needed on the web, someone with a little originality. useful job for bringing something new to the internet! Visit my website  <a href=”http://www.referaty.pl/prace-licencjackie.html”>prace magisterskie</a>. Thank You very much!

  • Tiesha Itzkowitz

    2/7/2011 8:33:56 AM |

    Of course, what a fantastic blog and revealing posts, I definitely will bookmark your website.Best Regards!

  • maple leaf coins

    2/7/2011 11:19:40 AM |

    F*ckin' remarkable issues here. I'm very happy to peer your post. Thanks a lot and i am taking a look ahead to touch you. Will you please drop me a mail?

  • stomatolog Twardowice

    2/7/2011 11:20:51 PM |

    Hello there,  You have done an incredible job. I will definitely digg it and personally recommend to my friends. I'm confident they will be benefited from this website.

  • stomatolog Sorkwity

    2/7/2011 11:24:53 PM |

    Somebody essentially help to make seriously articles I would state. This is the first time I frequented your website page and thus far? I amazed with the research you made to create this particular publish amazing. Fantastic job!

  • Turbosprężarki

    2/7/2011 11:26:28 PM |

    I do agree with all the ideas you've presented in your post. They are very convincing and will definitely work. Still, the posts are very short for novices. Could you please extend them a bit from next time? Thanks for the post.

  • montaż szyb samochodowych warszawa

    2/7/2011 11:37:30 PM |

    I am also commenting to make you know what a remarkable discovery my cousin's daughter experienced visiting your site. She discovered several issues, including what it is like to have an incredible giving spirit to let a number of people with no trouble comprehend various tortuous subject areas. You undoubtedly surpassed our own desires. I appreciate you for presenting those great, trustworthy, explanatory and in addition unique thoughts on that topic to Lizeth.

  • live seahorses sale

    2/8/2011 2:34:10 AM |

    Mistakes are a part of being human. Appreciate your mistakes for what they are: precious life lessons that can only be learned the hard way. Unless it's a fatal mistake, which, at least, others can learn from.

  • lego

    2/8/2011 11:08:17 PM |

    Thank you for your article.

  • wymiana szyb samochodowych warszawa

    2/9/2011 1:57:28 AM |

    Hey There. I found your blog using msn. This is a really well written article. I will be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I will definitely return.

  • online poker

    2/9/2011 11:50:39 PM |

    I will not hesitate to propose the sites to anyone who needs and wants direction about this matter.

  • Gotowe Prace

    2/10/2011 5:10:40 PM |

    I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!… Come and see my webpage  <a href=”http://www.pisanieprac.pl/prace-licencjackie.html”>prace magisterskie</a>. See You.

  • Thu Weedon

    2/11/2011 6:56:42 AM |

    Wow! Thank you! I constantly needed to write on my blog something like that. Can I take a part of your post to my website?

  • olympus pink digital camera

    2/13/2011 5:23:22 PM |

    We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on. You have done a marvellous job!

  • stomatolog Wolin

    2/14/2011 5:33:17 AM |

    Hi there,  You have done a fantastic job. I’ll definitely digg it and personally suggest to my friends. I'm sure they will be benefited from this website.

  • Katalog Stron

    2/17/2011 7:54:01 PM |

    I was recommended this website by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You are incredible! Thanks!

  • Ezine Articles

    2/20/2011 8:40:51 AM |

    I thought you were going to chip in with some decisive insigth at the end there. Not leave it with 'we leave it to you to decide'

  • Eugena Minacci

    2/20/2011 8:57:40 AM |

    I just want to tell you that I am beginner to blogging and honestly loved you're page. Very likely I’m going to bookmark your blog post . You certainly have superb article content. With thanks for sharing with us your webpage.

  • Tod Joles

    2/20/2011 10:53:11 AM |

    I just want to mention I'm new to blogs and actually enjoyed your web site. More than likely I’m want to bookmark your blog post . You really have excellent stories. Thanks a lot for revealing your web site.

  • kodeki pobierz

    2/23/2011 6:48:29 PM |

    I wanna read more your articles!

  • beautiful korean women

    2/24/2011 2:40:37 PM |

    Many thanks for giving the warm and friendly, dependable, explanatory as well as fun thoughts on your topic.

  • Luigi Meusa

    3/10/2011 8:25:40 AM |

    You made a few good points there. I did a search on the topic and found the majority of persons will go along with with your blog.

  • cholesterin

    3/15/2011 12:50:47 PM |

    I am totally new to blogging and at the moment I am not yet sure what blog platform I should use? Friends have recommended me to try out Blogspot. Would you say BlogEngine is more powerful than afore-said blog tool?

  • Jacinto Wohlwendi

    3/19/2011 3:52:37 AM |

    Would you be desirous about exchanging links?

  • Nike Air Max Lebron VII

    3/24/2011 1:32:32 PM |

    Ha, that? Surely a Genuinely meet suggestion. Numerous thanks so much for this!

  • Homme Nike Air Max LTD

    3/25/2011 12:17:39 PM |

    Here's hoping there's a an enormous number more top-notch supplies coming!

  • Tony Esposito Jerseys

    3/26/2011 4:09:05 AM |

    Hello may I extract some of the judgement from this post if I accord a relation back to your site?

  • Wholesale Versace Sunglasses

    3/30/2011 1:18:24 PM |

    I'm young on here, I hit upon this website I find It melodic helpful and its helped me out a lot. I will be expert to forward & succour other users like it has helped me.

  • Air Jordan 8

    3/31/2011 1:29:42 PM |

    Hi there, I dependably like reading your posts, offer you!

  • Homme Nike Air Max 2012

    4/1/2011 10:18:41 PM |

    I harmonise with your conclusions and will thirstily look progressive to your coming updates.

  • Wholesale Chanel Sunglasses

    4/4/2011 2:26:09 PM |

    Smashing Blog. I add this Blog to my bookmarks.Thanks against taking the time to argue this, I lean to strongly about it and light of one's life learning more on this topic.

  • Joann O. Coble

    4/5/2011 5:07:09 AM |

    Hi everyone, - came upon your webpage by pure luck while searching across the internet this morning, and glad that i did! I do like the design and style and colours, but I need to point out that I'm having problems when it loads. I'm making use of Sunrise 2 browser for mac, and the header does not line up well. i'm confident  made use of precisely the same design on a client's internet site, but the menu seems okay on mine. I imagine the mistake is at my end & I suppose it is time to upgrade!

  • Air Max 180

    4/6/2011 10:27:25 AM |

    I'm late-model on here, I hit upon this website I repossess It charming supportive and its helped me revealed a lot. I will be accomplished to forward & aid other users like it has helped me.

  • Air Max 24/7

    4/8/2011 12:58:52 AM |

    Hi there can I bring into play some of the insight here in this inlet if I provide a fasten together underwrite to your site?

  • Cristobal Huet Jerseys

    4/20/2011 6:28:16 AM |

    Someone I jobless with visits your orientation commonly and recommended it to me to read also.

  • Online Seo Company

    5/24/2011 3:25:49 PM |

    nice blog, I like it
    Thanks, keep the good work.

  • breast enhancement photos

    6/1/2011 6:44:33 AM |

    Can I put up your submit to my wordpress weblog? I will add a one-way link to your forum. That’s one actually nice post.

  • colon cleansing foods

    6/1/2011 1:28:31 PM |

    You ought to actually take into consideration working on creating this blog into a significant authority in this market. You evidently have a grasp handle of the subjects everyone seems to be searching for on this website anyhow and you would definitely even earn a buck or two off of some advertisements. I might explore following recent matters and raising the amount of write ups you place up and I assure you’d start seeing some wonderful focused site visitors within the close to future. Just a thought, good luck in whatever you do!

  • lipfusion lip plumper

    6/2/2011 11:13:13 AM |

    It had been some time since I visited web site with such high quality information. Thansk so much for the helpful data

  • homemade lip plumper

    6/2/2011 2:04:33 PM |

    I have been examinating out a few of your stories and i must say clever stuff. I will definitely bookmark your site.

  • penis enlarging pills

    6/3/2011 2:34:15 AM |

    I'm still learning from you, but I'm making my way to the top as well. I absolutely liked reading everything that is posted on your blog.Keep the aarticles coming. I enjoyed it!

  • prevent stretch marks

    6/3/2011 8:14:58 AM |

    Thanks for an concept, you sparked at thought from a angle I hadn’t given thoguht to yet. Now lets see if I can do something with it.

  • sites

    6/4/2011 7:45:49 AM |

    wonderful post.. this was said very good

  • sites

    6/4/2011 7:50:17 AM |

    wonderful post.. this was said very good

  • Gayle Kitchell

    6/4/2011 8:12:23 AM |

    Right-minded saying thanks settle upon not reasonable be enough, suited for the wonderful definition in your writing.

  • x ray technician salary

    6/5/2011 5:55:35 PM |

    Awsome post and straight to the point. I don't know if this is in fact the best place to ask but do you people have any ideea where to employ some professional writers? Thanks in advance Smile

  • watches

    6/10/2011 8:30:00 AM |

    Hello there! Great post! But this website is loading slowly.

  • watches

    6/10/2011 11:33:01 PM |

    Hello! Cool post! But the site has been loading rather slowly.

  • wholesale Kings jerseys

    6/13/2011 1:03:42 AM |

    hey, nice blog…absolutely like it and added to bookmarks. keep up with admissible in the works!

  • Zähne bleachen

    6/13/2011 5:31:10 PM |

    Sorry for the offtopic question but would you please tell me where I can get the site theme that you use? I'd like to get the same or maybe a very similar one. Could you please tell me who made it?

  • wholesale Carolina jerseys

    6/15/2011 6:26:33 AM |

    Reading at near means of your enjoyable enter, will-power assist me to do so sometimes.

  • wholesale Steelers jerseys

    6/16/2011 11:07:53 PM |

    If conceivable, as you glean knowledge, would you brainpower updating your blog with more information? It is exceptionally helpful for me.

  • wholesale Steelers jerseys

    6/19/2011 6:21:43 PM |

    I worth the superior post you allotment in your articles. I'll bookmark your blog and keep my readers scan here often.

  • Nike Air Jordan 17

    6/24/2011 11:21:23 AM |

    Here's hoping there's a lot more top-notch means coming!

  • top male enhancement

    6/27/2011 5:09:03 AM |

    Do you people have a fb fan web page? I regarded for one on twitter however could not uncover one, I would like to become a fan!

  • quick colon cleanse

    6/28/2011 11:55:00 AM |

    Right now, many health concerns arise such as stomach pain, bowel problems, as well as deficiency of energy are usually related with an unhealthy colon. This is largely because of our poor eating plan which trigger waste products to accumulate within the digestive tract triggering specific medical problems to arise. In order to do away with these the natural and safe method, then try out colon cleansing in your own home, proven and tested effective.

  • natural colon cleanse

    6/29/2011 3:56:20 AM |

    Determining the best colon cleansers sold these days could be very tricky especially for a newbie. This is primarily because you'll find loads of these items in the market today. But as long as you really know what to look in the product, then it’ll be simple for you. A few significant things to examine are the list of components, product critiques, the producer, along with the promos they feature.

  • Talitha Framer

    6/29/2011 4:51:08 AM |

    hey, punctilious blog…absolutely like it and added to bookmarks. victual up with good go well!

  • Air Jordan 4

    6/29/2011 7:44:02 PM |

    The handwriting craze is exceptional and the cheer is relevant. Thanks benefit of the perspicaciousness you cater the readers!

  • eyelash lengthening

    6/30/2011 1:08:17 AM |

    In most cases, females would want to look lovely and appealing always. This is one reason why even the eyelashes can now be improved via using eyelash lengtheners. These are products that will help any women attain fuller, thicker as well as longer eyelashes which could definitely get anybody’s attention. These items have been guaranteed safe, effective and cost effective given that the appropriate item is chosen and bought.

  • how to get long eyelashes

    6/30/2011 3:07:04 AM |

    Most of the time, girls would like to look wonderful and appealing always. This is one reason why even the lashes now can be improved via using eyelash lengtheners. These are goods that can assist any women obtain fuller, thicker and lengthier lashes which could definitely get anybody’s interest. These products have been guaranteed safe, effective and budget friendly provided that the best item is selected and bought.

  • Burton Haynes

    6/30/2011 7:06:21 AM |

    I simply want to tell you that I am just all new to blogging and definitely savored your web page. More than likely I’m going to bookmark your website . You definitely have exceptional stories. Regards for sharing your website page.

  • Andrew A. Sailer

    7/1/2011 5:25:25 AM |

    I simply want to mention I am just all new to blogging and seriously loved your blog site. Likely I’m want to bookmark your site . You actually come with excellent stories. With thanks for sharing with us your web site.

  • Air Max 95

    7/7/2011 4:10:26 AM |

    Advantageously, the advise is in authenticity the sweetest on this merited topic.

  • Issac Maez

    7/8/2011 4:52:23 AM |

    Hey could I services some of the discernment from this blog if I accommodate a tie-in back to your site?

  • Air Max Skyline

    7/9/2011 4:07:45 AM |

    Hello may I exigency execrate some of the information here in this entry if I identify with backside to you?

  • Coach Outlet

    7/9/2011 11:09:59 AM |

    Someone I piece with visits your blog quite again and recommended it to me to comprehend too. The letters craze is considerable and the content is interesting. Thanks for the understanding you lay down the readers!

  • wholesale Brewers jerseys

    7/18/2011 1:00:33 AM |

    Wow, that looks so seemly!Perhaps you fool changed your point not too covet ago? For all practical purposes because I could take sworn it was not the having said that the previous time I was here…unless I am theory of a bizarre net site.

  • Georgine Fleig

    7/19/2011 4:53:03 AM |

    Hello! Nice post! But the site has been loading pretty slowly.

  • tory burch outlet

    7/19/2011 12:13:10 PM |

    I longing more people would make a note sites like this that are in actuality accommodating to read. With all the balls up floating around on the net, it is rare to look over a site like yours instead.

  • Kenneth Raker

    7/19/2011 1:15:41 PM |

    Hey there! Cool post! But the site is still loading incredibly slowly.

  • Air Max Classic BW

    7/24/2011 12:18:22 AM |

    Hello may I extract some of the perception from this delivery if I cater a bond back to your site?

  • Femme Nike Air Max 360

    7/24/2011 6:05:27 AM |

    Here's hoping there's a a load more top-notch material coming!

  • skeleton watches men

    7/24/2011 10:06:15 AM |

    Hello! Fine post! Please do tell us when I will see a follow up!

  • skeleton watches

    7/24/2011 12:28:11 PM |

    Hello! Fine post! Please  when we can see a follow up!

  • montre gps running

    7/25/2011 7:24:45 AM |

    Hello! Cool post! Please do inform us when I could see a follow up!

  • montre gps

    7/25/2011 11:34:25 AM |

    Hello! Great stuff, please keep us posted when you post again something like this!

  • skeleton watch

    7/27/2011 1:43:31 AM |

    Hello there! Wonderful post! Please inform us when we can see a follow up!

  • garmin forerunner 305

    7/27/2011 3:52:10 AM |

    Hello! Wonderful stuff, please tell me when you finally post something similar!

  • Santos Cantine

    7/27/2011 4:25:21 AM |

    Hello there! Fine post! But this blog is still loading pretty slowly.

  • Dominic Hohenberger

    7/27/2011 6:19:17 AM |

    Hi there! Great post! But the blog has been loading incredibly slowly.

  • Faustino Tarro

    7/27/2011 10:53:42 AM |

    Hi! Wonderful post! But the webpage has been loading very slowly.

  • garmin forerunner 305

    7/27/2011 11:35:29 AM |

    Hey! Cool post! Please tell us when all will see a follow up!

  • Epifania Siemsen

    7/27/2011 12:20:17 PM |

    Hello there! Wonderful post! But the website has been loading pretty slowly.

  • Winfred Medill

    7/27/2011 1:24:21 PM |

    Hey! Great post! But the website has been loading slowly.

  • Ernie Schellin

    7/27/2011 1:58:41 PM |

    Hi! Wonderful post! But this webpage has been loading pretty slowly.

  • Leslie Cassard

    7/27/2011 3:03:03 PM |

    Hi! Nice post! But the website is always loading pretty slowly.

  • garmin forerunner

    7/27/2011 8:03:13 PM |

    Hi there! Nice post! Please tell us when all shall see a follow up!

  • Reid Darke

    7/27/2011 8:46:42 PM |

    Hey! Great post! But this website is always loading rather slowly.

  • Ezekiel Boylston

    7/27/2011 9:01:02 PM |

    Hello there! Fine post! But the website is always loading very slowly.

  • Cecil Hosseini

    7/27/2011 10:54:36 PM |

    Hello! Good post! But the webpage is loading very slowly.

  • montres femmes

    7/27/2011 11:02:00 PM |

    Hello there! Nice stuff, please inform us when you finally post something like this!

  • garmin forerunner 305

    7/27/2011 11:52:19 PM |

    Hey there! Wonderful stuff, do inform us when you post again something similar!

  • montres femmes guess

    7/28/2011 2:38:48 AM |

    Hello there! Wonderful post! Please tell us when we will see a follow up!

  • forerunner 305

    7/28/2011 4:35:31 AM |

    Hi! Wonderful stuff, please do tell us when you post again something like this!

  • womens ceramic watches

    7/28/2011 6:05:17 AM |

    Hi there! Wonderful post! Please inform us when we will see a follow up!

  • automatic watches

    7/28/2011 7:50:37 AM |

    Hey there! Wonderful post! Please tell us when we shall see a follow up!

  • g shock white watch

    7/28/2011 8:00:51 AM |

    Hey there! Nice stuff, please do keep us posted when you finally post something like this!

  • ceramic watches

    7/28/2011 8:07:41 AM |

    Hey there! Good stuff, please inform me when you finally post something like that!

  • forerunner 305

    7/28/2011 8:19:30 AM |

    Hi! Great post! Please do inform us when I could see a follow up!

  • womens white watch

    7/28/2011 8:21:54 AM |

    Hi! Fine post! Please inform us when we will see a follow up!

  • ceramic watches for women

    7/28/2011 2:40:44 PM |

    Hello! Great stuff, please tell me when you publish again something similar!

  • ceramic watches

    7/28/2011 4:31:40 PM |

    Hi! Wonderful stuff, please tell me when you publish again something similar!

  • click here

    7/28/2011 4:36:39 PM |

    Hey there! Good stuff, please inform me when you post again something like that!

  • automatic watches

    7/28/2011 4:36:39 PM |

    Hello there! Great stuff, please keep me posted when you finally post something like that!

  • robot menager kenwood

    7/28/2011 5:31:55 PM |

    Hi! Wonderful post! Please do keep us posted when all shall see a follow up!

  • robot multifonction

    7/28/2011 5:58:27 PM |

    Hey! Cool post! Please inform us when I will see a follow up!

  • automatic watches

    7/28/2011 5:59:17 PM |

    Hi there! Good post! Please  when I could see a follow up!

  • white watch

    7/28/2011 5:59:17 PM |

    Hey there! Nice stuff, do tell us when you post something similar!

  • robot multifonction

    7/28/2011 7:22:38 PM |

    Hi there! Wonderful stuff, please keep me posted when you finally post something like that!

  • ceramic watches for women

    7/28/2011 11:45:15 PM |

    Hello there! Wonderful stuff, do inform us when you finally post something similar!

  • montre femme guess

    7/29/2011 3:59:49 AM |

    Hey! Great stuff, do keep me posted when you finally post something like that!

  • cliquez ici

    7/29/2011 4:30:28 AM |

    Hello there! Wonderful stuff, do inform me when you finally post something like this!

  • Lieselotte

    7/30/2011 9:58:10 AM |

    Practice makes perfect.

  • white watch

    7/31/2011 7:54:25 PM |

    Hi! Wonderful stuff, please do inform us when you publish again something like that!

  • white watch

    7/31/2011 11:28:29 PM |

    Hi! Fine post! Please do tell us when I can see a follow up!

  • chanel ceramic watches

    8/1/2011 4:45:52 AM |

    Hey there! Nice post! Please do inform us when we shall see a follow up!

  • table a langer pas cher

    8/1/2011 12:17:15 PM |

    Hey there! Fine post! Please do tell us when I could see a follow up!

  • cliquez ici

    8/1/2011 3:19:59 PM |

    Hi there! Fine post! Please tell us when all could see a follow up!

  • white watches

    8/1/2011 10:18:59 PM |

    Hey! Wonderful stuff, do inform us when you post something similar!

  • white watches

    8/2/2011 2:01:52 AM |

    Hello there! Great post! Please do inform us when I shall see a follow up!

  • chanel white ceramic watch

    8/2/2011 9:48:05 AM |

    Hey! Great stuff, please keep us posted when you publish again something like this!

  • montre femme guess

    8/5/2011 6:27:02 AM |

    Hey! Wonderful stuff, please do keep us posted when you finally post something like this!

  • decodeur tnt satellite

    8/5/2011 6:52:01 AM |

    Hi! Good stuff, please do keep me posted when you post again something similar!

  • guess montre femme

    8/5/2011 7:59:37 AM |

    Hi there! Good stuff, do inform us when you finally post something like that!

  • lit parapluie

    8/6/2011 2:42:04 AM |

    Hey there! Good post! Please inform us when all will see a follow up!

  • lit parapluie leclerc

    8/6/2011 3:36:55 AM |

    Hi there! Fine post! Please do keep us posted when I can see a follow up!

  • Air Max Classic BW

    8/6/2011 10:59:04 PM |

    Hi there, I honestly like reading your posts, sometimes non-standard due to you!

  • Monster Beats

    8/7/2011 2:16:48 AM |

    Wow, that looks so beneficial!Perhaps you give birth to changed your theme not too long ago? Mainly because I could take sworn it was not the unchanging the one-time every so often old-fashioned I was here¡­unless I am intelligent of a different trap site.

  • Tory Burch Heels

    8/7/2011 5:53:53 AM |

    Say thank you you for another remarkable article. Where else could anybody suffer from that well-intentioned of bumf in such a gargantuan practice of writing.

  • montres homme

    8/8/2011 12:11:02 PM |

    Hello there! Cool post! Please inform us when I can see a follow up!

  • decodeur tnt hd

    8/8/2011 6:26:01 PM |

    Hello! Good stuff, do inform us when you post something like this!

  • velo enfant

    8/9/2011 7:48:36 PM |

    Hey there! Good stuff, do inform me when you finally post something like this!

  • velo enfants

    8/10/2011 12:58:27 AM |

    Hello! Good post! Please do inform us when we can see a follow up!

  • ceramic watch

    8/10/2011 11:24:06 PM |

    Hi! Great stuff, please do tell me when you finally post something like that!

  • cardiofrequencemetre polar

    8/10/2011 11:28:23 PM |

    Hey! Nice stuff, please do inform us when you post again something like that!

  • siege auto bebe

    8/10/2011 11:37:24 PM |

    Hi there! Fine post! Please do keep us posted when all will see a follow up!

  • ceramic watch

    8/11/2011 5:50:15 AM |

    Hey there! Good stuff, please do tell me when you publish again something like this!

  • cardiofrequencemetre

    8/11/2011 7:58:09 AM |

    Hello! Great post! Please do keep us posted when I will see a follow up!

  • siege auto bebe

    8/11/2011 11:11:50 AM |

    Hello! Nice stuff, please inform us when you publish again something similar!

  • exercise and fitness tips

    8/11/2011 3:39:48 PM |

    Many benefits are an interesting matter mainly because it looks like everyone looks to another country for answers and definitely the same holds true throughout the United States. In fact, your controversial topic associated with health advantages has been and also may be such a scorching matter the idea perfectly may at the very least somewhat make a decision that our subsequent leader will probably be.

  • garmin forerunner 305

    8/11/2011 5:03:09 PM |

    Hello there! Wonderful stuff, please do keep me posted when you post something similar!

  • Tory Burch Flats

    8/12/2011 9:57:50 AM |

    Extremely beneficial, I intent definately be returning because i start my consequent after task.

  • montre hello kitty

    8/12/2011 1:13:39 PM |

    Hey! Wonderful stuff, please keep me posted when you post again something like that!

  • wholesale Blues jerseys

    8/12/2011 1:42:56 PM |

    Thank you in return another staggering article. Where else could anybody pry out that benevolent of poop in such a great practice of writing.

  • plaque a induction

    8/13/2011 2:57:16 PM |

    Hello! Nice blog post! I have been a daily visitor (far like addict Tong) to your website even though I had a question. I am only not likely certain if its the right place to ask, but you've got no spam comments. I get comments often. Are you going to you help me? Many thanks!

  • automatic watch

    8/13/2011 6:41:35 PM |

    Hello there! Cool post! Please do tell us when I will see a follow up!

  • bureau enfant

    8/13/2011 10:18:34 PM |

    Howdy! Useful blog! I'm also a daily website visitor (a bit more like addict Tong) of your website but also I had a question. I am just not necessarily quite pretty sure whether its the right place to question, but there are no spam comments. I receive comments everyday. Will you help me? Thanks a lot!

  • garmin forerunner

    8/14/2011 12:49:51 AM |

    Hi! Wonderful stuff, do inform me when you post something similar!

  • Media

    5/2/2013 2:03:06 AM |

    Thanks for these codes, they are useful

  • Phone Numbers

    3/17/2014 8:25:32 PM |

    The Numbers Helpline has sourced 1000's of customer service contact <a href="www.numbershelpline.co.uk/.../";>Phone numbers</a> in the UK. We know how hard it can be to find those <a href="www.numbershelpline.co.uk/.../";>Phonenumbers</a> at times that why we have made a directory with them all in one place so you will never have waste long periods of time looking for a customer service contact <a href="www.numbershelpline.co.uk/.../";>Phonenumbers</a> again.

  • Phone Numbers

    3/20/2014 9:31:25 PM |

    The Numbers Helpline has sourced 1000's of customer service contact <a href="www.numbershelpline.co.uk/.../";>Phone numbers</a> in the UK. We know how hard it can be to find those <a href="www.numbershelpline.co.uk/.../";>Phonenumbers</a> at times that why we have made a directory with them all in one place so you will never have waste long periods of time looking for a customer service contact <a href="www.numbershelpline.co.uk/.../";>Phonenumbers</a> again.

  • Phone Numbers

    3/21/2014 1:04:02 AM |

    The Numbers Helpline has sourced 1000's of customer service contact <a href="www.numbershelpline.co.uk/.../";>Phone numbers</a> in the UK. We know how hard it can be to find those <a href="www.numbershelpline.co.uk/.../";>Phonenumbers</a> at times that why we have made a directory with them all in one place so you will never have waste long periods of time looking for a customer service contact <a href="www.numbershelpline.co.uk/.../";>Phonenumbers</a> again.

  • Phone Numbers

    4/1/2014 12:53:50 AM |

    The Numbers Helpline has sourced 1000's of customer service contact <a href="www.numbershelpline.co.uk/.../";>Phone numbers</a> in the UK. We know how hard it can be to find those <a href="www.numbershelpline.co.uk/.../";>Phonenumbers</a> at times that why we have made a directory with them all in one place so you will never have waste long periods of time looking for a customer service contact <a href="www.numbershelpline.co.uk/.../";>Phonenumbers</a> again.

  • Phone Numbers

    4/28/2014 9:07:20 PM |

    The Numbers Helpline has sourced 1000's of customer service contact <a href="www.numbershelpline.co.uk/.../";>Phone numbers</a> in the UK. We know how hard it can be to find those <a href="www.numbershelpline.co.uk/.../";>Phonenumbers</a> at times that why we have made a directory with them all in one place so you will never have waste long periods of time looking for a customer service contact <a href="www.numbershelpline.co.uk/.../";>Phonenumbers</a> again.

  • Matthew C. Kriner

    5/17/2014 9:27:06 AM |

    I simply want to mention I'm beginner to weblog and actually liked you're web-site. More than likely I’m going to bookmark your website . You surely have exceptional articles. Appreciate it for revealing your blog.

  • Burton Haynes

    5/17/2014 1:25:35 PM |

    I just want to mention I am just newbie to blogs and definitely savored this web page. Almost certainly I’m planning to bookmark your website . You actually have perfect articles and reviews. Thank you for sharing your blog.

Loading

Using T4 to automatically generate your entities

Using T4 to automatically generate your entities

Today I discovered something very interesting, namely how to automatically generate my entity classes (classes that also implement the INotifyPropertyChanged and IDataErrorInfo interfaces) using T4.

T4 is a text generation language built into Visual Studio 2005 and later, normally intended for DSL code generation. But we can also use it for generating any code we would like. I for one, I would like automatic properties to automatically implement INotifyPropertyChanged if the interface is on the class, but of course, life doesn’t work that way *sigh*. The real problem is that for each property you have, part of the implementation needs to raise an event using the property name. When you change the name of the property you also have to change this string, which is error prone…

Instead we can generate code using T4, and to be honest, it isn’t that difficult to use. First of all, go to tangible to download their free T4 intelli-sense tool.

Next add to your project a new text file, naming it Entity.tt (the extension is important!). Now add this content:

<#@ template language="C#" #> <#@ output extension="cs" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.Collections.Generic" #> // --------------------------------- // U2U Sample, use at your own risk! //        http://www.u2u.be // ---------------------------------  using System; using System.ComponentModel; using System.Collections.Generic;  namespace <#= this.Namespace #>  {     public partial class <#= this.ClassName #>      : INotifyPropertyChanged     , IDataErrorInfo     {     #region Private fields     <# for (int idx = 0; idx < this.properties.GetLength(0); idx++) { #>         private <#= this.properties[idx,0] #> _<#= this.properties[idx,1].ToLower() #>;<# }#>             #endregion          #region Properties     <# for (int idx = 0; idx < this.properties.GetLength(0); idx++) { #>         public <#= this.properties[idx,0] #> <#= this.properties[idx,1] #>     {               get       {                     return _<#= this.properties[idx,1].ToLower() #>;               }               set               {                     _<#= this.properties[idx,1].ToLower() #> = value;         OnPropertyChanged("<#= this.properties[idx,1]#>");               }        }     <#   }   #>           #endregion          #region INotifyPropertyChanged Members             public event PropertyChangedEventHandler PropertyChanged;          protected virtual void OnPropertyChanged(string propertyName)        {              if (PropertyChanged != null)                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));        }             #endregion          #region IDataErrorInfo Members          private string _error = null;          public string Error {       get       {          return _error;        }       set        {         _error = value;       }     }          private Dictionary<string,string> _columnErrors =       new Dictionary<string,string>();            public string this[string columnName]     {       get       {         if( _columnErrors != null && _columnErrors.ContainsKey(columnName))           return _columnErrors[columnName];         else           return null;       }       set       {         if( value != null ) // Insert error         {           if( _columnErrors == null )           {             _columnErrors = new Dictionary<string,string>();           }           _columnErrors[columnName] = value;         }         else // Clear error         {           _columnErrors.Remove(columnName);           if (_columnErrors.Count == 0)           {             _columnErrors = null;           }         }         // Notify validation that something has changed         OnPropertyChanged( columnName );       }     }     #endregion   } }  <#+       string Namespace = "Demo";       string ClassName = "DemoClass";       string[,] properties = {             {"int", "Property1"},             {"string", "Property2"}   };                #>

Building your project will generate a little DemoClass with these two properties. But that is of course not what you want; so add another file Category.tt:

<#       this.Namespace = "U2U.Samples";       this.ClassName = "Category";       this.properties = new string[,]   {             {"int"   , "CategoryID"}   , {"string", "CategoryName"}         , {"string", "Description"}         , {"byte[]", "Picture"}   };  #>  <#@ include file="Entity.tt" #>  

Building will now automatically generate a Category.cs file with code like this:

// --------------------------------- // U2U Sample, use at your own risk! //        http://www.u2u.be // ---------------------------------  using System; using System.ComponentModel; using System.Collections.Generic;  namespace U2U.Samples {   public partial class Category     : INotifyPropertyChanged     , IDataErrorInfo   {     #region Private fields      private int _categoryid;     private string _categoryname;     private string _description;     private byte[] _picture;      #endregion      #region Properties      public int CategoryID     {       get       {         return _categoryid;       }       set       {         _categoryid = value;         OnPropertyChanged("CategoryID");       }     }      public string CategoryName     {       get       {         return _categoryname;       }       set       {         _categoryname = value;         OnPropertyChanged("CategoryName");       }     }      public string Description     {       get       {         return _description;       }       set       {         _description = value;         OnPropertyChanged("Description");       }     }      public byte[] Picture     {       get       {         return _picture;       }       set       {         _picture = value;         OnPropertyChanged("Picture");       }     }      #endregion      #region INotifyPropertyChanged Members      public event PropertyChangedEventHandler PropertyChanged;      protected virtual void OnPropertyChanged(string propertyName)     {       if (PropertyChanged != null)         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));     }      #endregion      #region IDataErrorInfo Members      private string _error = null;      public string Error     {       get       {         return _error;       }       set       {         _error = value;       }     }      private Dictionary<string, string> _columnErrors =       new Dictionary<string, string>();      public string this[string columnName]     {       get       {         if (_columnErrors != null && _columnErrors.ContainsKey(columnName))           return _columnErrors[columnName];         else           return null;       }       set       {         if (value != null) // Insert error         {           if (_columnErrors == null)           {             _columnErrors = new Dictionary<string, string>();           }           _columnErrors[columnName] = value;         }         else // Clear error         {           _columnErrors.Remove(columnName);           if (_columnErrors.Count == 0)           {             _columnErrors = null;           }         }         // Notify validation that something has changed         OnPropertyChanged(columnName);       }     }     #endregion   } }  So to change/add/remove a property, we edit the Category.tt file and all the rest is done automatically.  Now for some feedback, what is your way of implementing IDataErrorInfo?

Comments (29) -

  • Orval Roadruck

    9/19/2013 10:21:14 PM |

    Hi, I do believe this is an excellent web site. I stumbledupon it ;) I'm going to come back yet again since I bookmarked it. Money and freedom is the greatest way to change, may you be rich and continue to guide other people.|

  • Chinese Restaurant Supply

    10/15/2013 11:51:32 AM |

    Heya i am for the first time here. I came across this board and I find It truly useful & it helped me out much. I hope to give something back and aid others like you helped me.|

  • DEBORA Laurence

    8/24/2014 1:05:39 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 1:27:15 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 2:03:37 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 5:16:12 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 5:17:36 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 5:43:49 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 6:20:17 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 6:39:00 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 7:51:41 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 8:41:42 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 8:42:28 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 8:46:28 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 9:33:11 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 9:48:47 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 9:55:53 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 12:44:27 PM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 2:20:05 PM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 3:15:13 PM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 6:44:04 PM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 7:08:40 PM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 7:21:01 PM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 7:54:52 PM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 10:53:02 PM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/24/2014 11:26:09 PM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/25/2014 1:09:15 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/25/2014 1:28:19 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

  • DEBORA Laurence

    8/25/2014 2:48:55 AM |

    annuaires-gratuit.com/ vous propose de créer gratuitement un annuaire de sites internet pour un bon référencement.

Loading

File IO in Windows 8

File IO in Windows 8

Let’s leave Windows Phone for a moment, and let’s have a look at Windows 8. Recently the U2U-team was present at the Microsoft Build-convention in Anaheim, California, where Microsoft unveiled Windows 8. Now, what we saw is still just a preview, but it clearly shows the direction in which Windows is evolving. Have a look at this video, to see Windows 8 in action :

 

The “new style” applications Windows 8 offers are “Metro”-applications (yes, like in Windows Phone Metro style), and these can be made using Xaml and managed code, or even using HTML5 + Javascript. Metro-apps will have to be installed through a marketplace, and are kind of sandboxed (Comparable to Silverlight, but different ;-)  ). One of the effects of this sandboxing is that your options for working with files are limited. You cannot just access any folder on your machine ! Unfortunately you’re also unable to use Isolated Storage. So what can you use ? Let’s start by writing info into the “Local Folder”.

Windows.Storage.ApplicationData appData = Windows.Storage.ApplicationData.Current;
 
StorageFile file = await appData.LocalFolder.CreateFileAsync("EmployeeList.u2u");
Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite);

 

No ordinary File here, but a StorageFile which I have to open to write in. The IRandomAccessStream I get back gives me the possibility to create an input- (for reading) or output-stream (for writing).

IOutputStream output = stream.GetOutputStreamAt(0);

 

And then I got stuck. The IOutputStream gives me a WriteAsync-function, but it ask me for an IBuffer-object. How to write data ? Luckily there’s already some MSDN-documentation available. That tells me to use a DataWriter which takes the IOutputStream as a constructor-argument.

DataWriter writer = new DataWriter(output);
writer.WriteString("HERE GOES DATA");
output.FlushAsync();

 

Not there yet ! The code I wrote starts fro; the assumption that File IO is much like the “ordinary” file IO: flush the stream and done. In Metro we have to ”commit” the data in the writer, and I have to start the flushing, so this is the working code :

   1:  DataWriter writer = new DataWriter(output);
   2:  writer.WriteString("HERE GOES DATA");
   3:   
   4:  await writer.StoreAsync();
   5:  output.FlushAsync().Start();    
   6:  statusTxt.Text = "File Saved";

 

They could have made it simpler Smile.
By localfolder I assumed the bin/debug-folder. Nothing there. It seems my file is located in C:\Users\michael\AppData\Local\Packages\d64899f1-9800-470a-9cb3-fa89210f4941_qs0a8q7rnpy8j\LocalState.

How about reading the file ? Well, simply reverse your writing-logic (writer becomes reader, output becomes input, store becomes load, …)

   1:  Windows.Storage.ApplicationData appData = Windows.Storage.ApplicationData.Current;
   2:   
   3:  var file = await appData.LocalFolder.GetFileAsync("EmployeeList.u2u");
   4:  Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
   5:   
   6:  IInputStream input = stream.GetInputStreamAt(0);
   7:   
   8:  DataReader reader = new DataReader(input);
   9:   
  10:  var size = stream.Size;
  11:  await reader.LoadAsync((uint)size);
  12:  var data = reader.ReadString((uint)size);

 

There you go!

What about other folders than the “local” ? The KnownFolders=clqss gives me access to following locations :

  • Documents Library
  • Home Group
  • Media Server Devices (DLNA – Digital Living Network Alliance Devices, sounds interesting)
  • MusicLibrary
  • Pictures Library
  • Removable Devices
  • Videos Library

Let’s change the first 2 lines of my writing-code for using the documents library :

StorageFolder doclib = Windows.Storage.KnownFolders.DocumentsLibrary;
 
StorageFile file = await doclib.CreateFileAsync("EmployeeList.u2u");

 

As soon as you start running your code, it will fail on the first line (no exception though, simply stops). That’s because you explicitly have to give the app the capability to access the Document library. Double-clicking on Package.appxmanifest allows you to do that :

image

Still: this time your app will stop on the second line. You also need to associate your app with the u2u-fileextension. This is also done in the appxmanifest-file in the declarations-tab where we add a filetype association declaration :

image

There you go: the app works and saves my u2u-file in the doclibrary.

Loading