Silverlight Tutorial Part 8: Creating a Desktop Application using WPF

This is part eight of eight tutorials that walk through how to build a simple Digg client application using Silverlight 2.  These tutorials are intended to be read in-order, and help explain some of the core programming concepts of Silverlight.

Creating a Desktop Application Using WPF

Our goal with this last tutorial is a little different than the previous seven tutorials.  We actually aren't going to be running code in Silverlight with this tutorial - instead we will be using WPF and .NET 3.5.  We'll be taking the existing search application code we've written to run inside Silverlight in the browser, and re-use it to also run as a Windows desktop application.

Silverlight ships with a compatible API subset of what is in the full version of the .NET Framework.  One goal with this is to enable developers to learn a common programming model and tool-set and be able to re-use skills, code and content across RIA web applications, rich Windows desktop applications, and Office solutions.

Below are the steps I took to re-use our existing Silverlight application code (which runs in the browser) to build a Windows desktop application version (which runs outside the browser).

Step 1: Create a New WPF Desktop Application

We'll start by creating a new WPF Desktop Application using VS 2008.  We'll name it "DiggDesktopSample":

This will create a project inside VS with two files - an App.xaml, and a Window.xaml:

Note that this project structure is very similar to the Silverlight one we created in Tutorial 1 of this series (which had an App.xaml and a Page.xaml file).

Step 2) Copy Existing Digg Application Code into the WPF Application

We'll copy/paste our existing DiggApplication Silverlight code into our new DiggDesktopSample Windows project:

Step 3) Fix Up a Few Issues

I had to make two changes to get our existing Digg sample code to compile:

1) I needed to add a line to the networking code to manually specify a "user-agent" header.  The Digg REST API will return an error if no user-agent header is sent - and by default Windows applications don't send this if you use the WebClient API (whereas browser hosted applications will send it). 

2) I had to change the <HyperlinkButton> control in our user control to be a <TextBlock>.  This is a new control in Silverlight 2 that isn't in the full WPF yet (we will add them in the future though).  I did not have to change any of the code that worked with these controls, though, nor my network, LINQ to XML, nor databinding code.

Once I made these small tweaks the project compiled clean.

Step 4) Hosting the Digg Application in a Desktop Window

I then opened up the Windows1.xaml file in the desktop project (which is the default Window that loads when the application starts). 

I updated the Window title to be "Digg Desktop Version" and expanded the default Width and Height of the Window.

I then added our Page.xaml user control from our previous Digg Silverlight project as the root control in the Window.  This will cause it to be visible and loaded when the Window loads.  I did not have to change the Page class code or rename anything in it.  Because it derives from UserControl it can be hosted just fine inside any WPF Window or Control. 

Step 5) Running the Application

I was then able to run our new Digg Desktop Application. All functionality worked the same as the Silverlight version and the application behaved the same:

And when a story is selected from the list, the details user control is displayed:

There are a few cosmetic styling differences between the browser and desktop version.  This is primarily because WPF by default inherits default styles (fonts, colors, scroll bars, etc) based on the operating system theme a user currently has selected, whereas Silverlight has a default theme that we use for all operating systems.  If we wanted to make the desktop version and the browser version absolutely identical we could do so by being more explicit with our Styles and Control Templates - otherwise the desktop version will adapt slightly based on the user's OS theme.

Summary

We'll be coming out with more detailed notes and suggested best practice guidance for sharing code across Silverlight and WPF projects in the future.  I think you'll find that the skills and knowledge you learn when building Silverlight applications will transfer very well to full WPF projects.  We are also striving for a very high compatibility bar that will enable good code re-use across solutions, and enable controls, content and code to be easily shared and leveraged.

Hope this helps,

Scott

54 Comments

  • Honestly, for the amount of time and effort we've put into adopting and releasing WPF applications, the more similar Silverlight is to WPF, the better.
    Theoretically, if Silverlight is a subset of WPF, why couldn't you just use Silverlight User Controls inside a WPF app(assuming you're not using incompatible controls, as seen in the example)?

  • I have just read the tutorial from 1 to 8 and I have to say this looks great.

    I have not really used WPF before (basically because I am design challenged) and all previous articles I have seen on using it made it look far more complicated; I think I may try this now as soon as the beta comes out.

    Thanks.

  • One question - can I run a Silverlight application on the desktop, without having any .Net framework installed?

  • Hi Scott this is great. I have looked at xaml briefly before but i find it alot easier when you explain things. Do you plan on releasing these tutorials as a book? I think it would be a great way of packaging up all of your linq to xml, linq to sql, .net 3.5, silverlight, wpf, asp.net - listview..., asp.net ajax tutorials you have done over the last year.

  • >>Theoretically, if Silverlight is a subset of WPF, why >>couldn't you just use Silverlight User Controls inside a >>WPF app(assuming you're not using incompatible controls, >>as seen in the example)?

    Because there are running on different .NET framework. 1 regular .NET CLR that you know and love, another one mini CLR for Silverlight.

  • phew.. read all the eight in a couple of hours and guess what "I am lovin' it" Keep up the good work guys, you just rock!!!

  • Scott: When are you going to embed a MediaElement in your banner? Maybe make the clouds flow behind the Space Needle.. :)

  • Read 1-8 and I am blown away. Thanks for all the good content. Looking forward to more silverlight tutorials. Scott, any ETA on Beta 2?

  • I can't wait for the beta to come out. Excellent tutorial. Looks amazing!

  • Looks great Scott, thanks for the tutorial.
    Can't wait to start building a SL2 Virtual Earth application ;)

  • When we can use the beta?

  • Hi Scott! Great stuff, all eight of them. Looking forward to beta and final release of Silverlight, to get my hand dirty :)

    All the best!

  • this is the best tutorial from start to finish i've seen. seriously scott, you should release a book that provides a reference implemenation on how to build business applications (data entry and reporting) that demonstrates best practices on implementimg flow, layout, naviagation, printing and all the other topics you discussed in this tutorial (but go a little deeper). us the same format as you did in this tuturial. The modal usercontrol example really helped me understand how to implement dialogs in silverlight.

    in terms of Silverlight and WPF, this is the most exciting technology (along with .Net framework of course) that MS has released since Windows 3.1 (windows 3.x with virtual memory manager was a huge leap over windows 2.x). Yes, i've been developing apps on windows that far back. I am now officially drinking the MS Koolaid again! Thank you!

  • Would love to see more information about the network api of silverlight... looks incredible impressive. Thank you for this great tutorial!

  • thats just plain awsome :)
    great tutorial :) not overly complicated, consise, really illusrates the framework :) excelent :)

    again, cant wait till mix :O

  • Hi Scott,

    after completing the tutorial the first thing that comes to mind is: good job on the tutorial! It's very helpful in making you understand, not just silverlight 2.0, but also WPF in general. Secondly I think there might be a tiny error in the sample. Although since I completed the sample as an Xbap project and not a silverlight project, this error might not hold exist in your application. The problem I experienced was with the interaction between the WebClient and the digg service. It seems that the WebClient class doesn't automatically add a "user-agent" header to the request which the digg service requires. However, I don't know if it's because the silverlight WebClient class does add the header or if the digg service added the requirement for the "user-agent" recently. The error was fixed by adding:

    diggService.Headers.Add("user-agent", "Silverlight Sample App");

    But again... Good work, keep it up!

    Kind regards,
    Johannes

  • Scott you are awesome!

    Thanks for the posts - there the first thing I check every day.

    David Roh

  • Anyone else having trouble downloading the tutorial code sample images today? Made it through Part 3 yesterday, and now I'm stuck!

  • Images are back online.

    Great tutorial! Probably overwelmed your image host server.

    Scott, you just convinced me to look into this silverlight stuff. Looks like 2.0 really rocks.

  • Hi Miguel,

    >>>>>>>> is there a way u could send me the color codes you're using in visual studio? :)

    I'm using a slightly adapted version of Oren's theme that Scott Hanselman pointed to here: http://www.hanselman.com/blog/VisualStudioProgrammerThemesGallery.aspx

    Hope this helps,

    Scott

  • Hi Michael,

    >>>>>>>> Theoretically, if Silverlight is a subset of WPF, why couldn't you just use Silverlight User Controls inside a WPF app(assuming you're not using incompatible controls, as seen in the example)?

    Yes - assuming you are using the same set of controls that are in the full WPF this should just work. Note that all controls we put in SL we also plan to support in WPF going forward.

    Thanks,

    Scott

  • Hi Koger,

    >>>>>>> One question - can I run a Silverlight application on the desktop, without having any .Net framework installed?

    To enable application outside the browser you'll want to use the .NET Framework and WPF.

    Hope this helps,

    Scott

  • Hi Lee,

    >>>>>>> Hi Scott this is great. I have looked at xaml briefly before but i find it alot easier when you explain things. Do you plan on releasing these tutorials as a book? I think it would be a great way of packaging up all of your linq to xml, linq to sql, .net 3.5, silverlight, wpf, asp.net - listview..., asp.net ajax tutorials you have done over the last year.

    It is an interesting idea, and one I haven't really looked hard at doing before. I will have to think about it to see. Thanks for the suggestion!

    Scott

  • Hi Keith,

    >>>>>>> Scott: When are you going to embed a MediaElement in your banner? Maybe make the clouds flow behind the Space Needle.. :)

    LOL - I will have to see about that. :-)

    Thanks,

    Scott

  • Hi Jared,

    >>>>>> Read 1-8 and I am blown away. Thanks for all the good content. Looking forward to more silverlight tutorials. Scott, any ETA on Beta 2?

    Thanks - glad you liked them! We haven't announced a Beta2 ETA just yet. We are focused right now on getting Beta1 out the door and will then be looking at where we end up with that before finalizing the exact B2 date. I'll post more details once we know more.

    Thanks,

    Scott

  • Hi Brian,

    >>>>>> this is the best tutorial from start to finish i've seen. seriously scott, you should release a book that provides a reference implemenation on how to build business applications (data entry and reporting) that demonstrates best practices on implementimg flow, layout, naviagation, printing and all the other topics you discussed in this tutorial (but go a little deeper). us the same format as you did in this tuturial. The modal usercontrol example really helped me understand how to implement dialogs in silverlight.

    >>>>>> in terms of Silverlight and WPF, this is the most exciting technology (along with .Net framework of course) that MS has released since Windows 3.1 (windows 3.x with virtual memory manager was a huge leap over windows 2.x). Yes, i've been developing apps on windows that far back. I am now officially drinking the MS Koolaid again! Thank you!

    Thanks a bunch for the kind words - I really appreciate it! Glad I could help!

    Scott

  • Hi Johannes,

    >>>>>> after completing the tutorial the first thing that comes to mind is: good job on the tutorial! It's very helpful in making you understand, not just silverlight 2.0, but also WPF in general. Secondly I think there might be a tiny error in the sample. Although since I completed the sample as an Xbap project and not a silverlight project, this error might not hold exist in your application. The problem I experienced was with the interaction between the WebClient and the digg service. It seems that the WebClient class doesn't automatically add a "user-agent" header to the request which the digg service requires. However, I don't know if it's because the silverlight WebClient class does add the header or if the digg service added the requirement for the "user-agent" recently. The error was fixed by adding:

    >>>>>>> diggService.Headers.Add("user-agent", "Silverlight Sample App");

    Cool - thanks for catching that! I was faking it out a different way, but that is much cleaner!

    Thanks,

    Scott

  • Hi David,

    >>>>> Anyone else having trouble downloading the tutorial code sample images today? Made it through Part 3 yesterday, and now I'm stuck!

    Sorry about that - my server had a network problem for a few hours this afternoon. Should be fine now.

    Thanks,

    Scott

  • Hello Scott,
    a ton of thanks for this pre release, tutorials, This will make life very easy when it will actually comes to development. 1 question, as you mentioned that we will have sockets also..
    so can you put some light how we gona use that, will it be a direct call to any netowrk or something else...
    BUt again thanks for the tutes..
    bluearc

  • I've been following some of the recent things you've done, hoping to find a reason to be able to use them at work (although I do more dba stuff now). The SL2.0 beta, and the great way you've demonstrated how easy it is, is going to get me started on ideas for a reporting solution for my state agency. I can think of lots of ways this would not only look better than other methods, but also the ease and functionality seem to be so much better.

    Thanks for all the work and the tutorials, they have really opened up my eyes to the possibilities.

    Stephen

  • I can't wait to try this when Beta1 is available!

    Thanks a lot for this great step by step tutorial!

    Matt

  • Hi, Scott.

    SL 2 is indeed big from your excellent tutorials. I have a question - can SL 2 work with database and SQL (not SQL.net)?

    thanks
    tyler

  • Scott,
    Let me echo everyone else's sentiment... this was an excellent demo. Thank you!
    Question though: Any plans to include WCF (or a subset) into Silverlight? I'm glad to see support for calling web services but it would be great if I could just open a ChannelFactory on the client. Thanks again!

  • Is System.Data namespace now supported in jolt?

  • Hi, I'm stumped. Adobe just announced that AIR will be able to use local storage, run on the desktop, etc. (ok, it has always run on the desktop but...). Is there ANY way to explain why Silverlight can't do the same? To be honest, a lightweight, run-anywhere platform like AIR is very appealing to a lot of people. Silverlight seems to be so close...but what's up? Is it the brass, unwilling to make life easier for us developers? Or is it a technical issue? I hope to hear from you. The answer has been plaguing me...and I'm just wondering if this is a lock-in tactic to develop WPF, or just a major barrier for the Silverlight team to overcome in the (near?) future. Cheers.

  • Scott, fantastic tutorial - really great work. The product is definitely top of my mind for any new development.

    Any word on whether XPS support will be in the beta? It's a feature we could REALLY do with to help with form generation, label printing etc.

    Regards
    Alex

  • Hi bluearc,

    >>>>>>> so can you put some light how we gona use that, will it be a direct call to any netowrk or something else...

    You'll be able to use the same System.Net socket API that is in the .NET Framework for sockets calls in Silverlight.

    Thanks,

    Scott

  • Hi Tyler,

    >>>>>> SL 2 is indeed big from your excellent tutorials. I have a question - can SL 2 work with database and SQL (not SQL.net)?

    Typically with a RIA solution like Silverlight you won't access the database directly from the browser (for security reasons). Instead you'd call a web-service on a web-server and it would talk to the database and send back data to Silverlight. Silverlight can then make changes to the data and send it back to the web-server for it to commit the database updates.

    Hope this helps,

    Scott

  • Hi James,

    >>>>>>> Question though: Any plans to include WCF (or a subset) into Silverlight? I'm glad to see support for calling web services but it would be great if I could just open a ChannelFactory on the client.

    Yes - Silverlight Beta1 has a WCF client subset and support for Channels and ChannelFactories.

    Hope this helps,

    Scott

  • Hi Charlie,

    >>>>>>> Adobe just announced that AIR will be able to use local storage, run on the desktop, etc. (ok, it has always run on the desktop but...). Is there ANY way to explain why Silverlight can't do the same? To be honest, a lightweight, run-anywhere platform like AIR is very appealing to a lot of people.

    AIR runs outside the browser, like WPF does already today. WPF supports a much richer set of APIs then AIR does (3D, Document Flow, richer offline storage, Office integration, ability to build desktop services and shell extensions, etc). It is also a superset of the Silverlight programming model inside the browser.

    Hope this helps,

    Scott

  • Hi Alan,

    >>>>> Thanks for the excellent SL2 preview. I entered your Silverlight source code and converted it to a WPF app. I wrote up the changes I needed to make (a few more than you had explicitly listed) in a blog article with downloadable WPF source code and a VS2008 project. See: www.alancobb.com/.../TranslatingScottGusSilverlightDiggClientToWPF.aspx

    Cool - thanks a bunch!

    Scott

  • Hi Scott

    The reason that I'm asking this, is that I'm going to develop a desktop application, but the .Net 3.0 redistribution would be too big for my users.
    So could you please tell a bit more about if I really need the .Net 3.0 redistribution? Obviously .Net 2.0 wouldn't help me with the Silverlight WPF subset, could .Net 2.0 redis. and SilverLight 2, help me run an SilverLight 2 application on the desktop.


    >>>>>>> One question - can I run a Silverlight application on the desktop, without having any .Net framework installed?

    >>To enable application outside the browser you'll want to use the .NET Framework and WPF.

  • Hi,
    how can you guys be so calm, don't you know what you've done! Wow!
    How does page to page navigation work (Not between html pages but within the same silverlight control)?

  • It were a great starter series.
    Please post as much sample as possible.
    And if possible then please also make some starter kits which might really help.

  • When I am trying to call methods from web-service in silverlight, the intellisence is not show me a name method, but show me a name method plus Async and this method returns void. What I am doing wrong?

  • Silverlight subset question,

    The system.net.webclient in silverlight does not have the credentials property, how are you supposed to set the credentials on you webclient request? or it is possible to reference the full system.net dll as included in GAC inside your silverlight app?

    thx

  • Hi,

    Can anyone help me understand the things that need to be done in this Part 8? I'm new to WPF and Silverlight and there was just a little too much hand waving here for me to follow.

    I create a new WPF app and name it DiggDesktopSample. When it says to copy/paste the files from the Silverlight DiggSample - which files specifically is it refering to? The DiggStory.cs and StoryDetailsView.xaml make sense to me. Do you just copy the Page.xaml and Page.xaml.cs files as well and change their namespace? How about the App.xaml and App.xaml.cs?

    Then Step 3 - Fix Up a Few Issues. I get the xmlns namespace change. WaterMarkTextBox to TextBox - ok. And do you replace the Watermark="Topic..." with Text? Replace the HyperlinkButton with TextBlock.. what do you do with the <HyperlinkButton Content="{Binding Title}" NavigateUri="{Binding HrefLink}" ?

    Step 4 - add the Page.xaml user control as the root control.. not sure how to do that.

    I haven't gotten this to compile yet.. has anyone gotten this Part 8 to build/run?

    This whole series has been so awesome - sure would like to figure this last part out.

  • Opps - sorry - I did see the last page including the Window1.xaml which answered the last question regarding how to add the Page as the control.

  • Hi Scott,

    Thx for the tutorial, they are very straight to the point for a developer migrating from 1.1

    As Charlie said in a previous comment, i think it would be nice if we can open a xap file directly in the browser, the same way we can open a swf file right now.

  • Scott,
    I am looking into databinding scenarios for Silverlight. Currently, the flavor of choice seems to be the ADO Entity Framework and the ADO.Net services.
    However, you also got me hooked on LINQtoSQL.
    Right now, I have a fairly extensive LINQtoSQL generation model that builds an admin interface for data manipulation based on a combination of the DBML from SQL Metal and an extended DBML-like XML file that I mark up and process.
    Anyway, long story short. What is the best way to leverage LINQtoSQL in a Silverlight Application? I can expose my data through WebServices or 'pages' that serve up XML object maps, etc. I would like to know the best practice for keeping my 'tiers' together in a data driven silverlight application, and specifically, one built on a data tier of LINQtoSQL.

    Thanks,

    Frederick

  • hi
    great work, but can you please tell me how to run a WPF application from my browser..?
    Thanks
    Uday

  • to Bill Campbell and the other having the same issue with the Proxy...

    I had the same issue than you. Comparing the my code with the downloaded scott's one, i noticed this extra line in SearchBtn_Click function :

    diggService.Headers.Add("user-agent", "Silverlight Sample App");

    If i have well understood, this add something non empty to the user-agent field in the http header's resquest. Normally this field contains the browser engine supported when you run a request from a web browser.

    Best regards,


    Paul

    PS: Thanks to Scott for providing us this excellent tutorial !

  • I just finished your tutorial, I rarely leave comments but had to this time. Great job and thanks for leaving it out there for us. It was really well put together and distilled some key concepts that I can use to create all types of applications. Thanks very much and looking forward to more content from you .

    Simon

  • Scott: Excellent work. Do we expect to have Chart controls when SL 2 is released? See you in Orlando on April 21st.

Comments have been disabled for this content.