Silverlight Desktop Applications
One of the features many Silverlight developers are asking about is the ability to run an existing Silverlight application on the desktop. They want to make it a standalone application that can run outside of the browser. Mono team has come up with Moonlight desklets which allows to run Moonlight(Silverlight on Linux) to run as standalone applications on the desktop. They also requested MS to build support for this to Silverlight to make cross-platform Silverlight desktop a reality.
After pondering about how to make Silverlight run as a standalone application on Windows for sometime , I came up with an easy way to accomplish this using HTML Applications(HTA). This article outlines the steps of creating a simple Silverlight desktop application.
Steps
1. Fire up Visual Studio 2008. Create a new Silverlight project named “HelloSilverLightDesktop” as shown below.
2. We will then be prompted to chose the host for our Silverlight application. We will chose to host our Silverlight application in an HTML page. We have not chosen to host it in a web project as we want to run as a standalone application on the desktop.
3. Open the Page.xaml page and copy the code below.
<UserControl x:Class="HelloSilverLightDesktop.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock>
Hello World from Silverlight Desktop Application!
</TextBlock>
</Grid>
</UserControl>
4. Build the application. You should see a similar directory structure.
Certain files are worth mention here:
Xap file: This file is a compiled Silverlight application. It is actually a .zip file which packages all the files necessary for the application.
TestPage.html: This file hosts the silverlight application. This file has a silverlight plugin whose source property points to the xap file as shown below:
<div id="silverlightControlHost">
<object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" width="100%" height="100%">
<param name="source" value="HelloSilverLightDesktop.xap"/>
</object>
</div>
5. We will now create a new shortcut on the desktop as shown below.
When we click this shortcut we will run mshta.exe <Silverlight application host html>.
What are we doing here?
An HTML application(HTA) is a windows application written using HTML. A normal HTML page is confined to security model of web browser, where as HTA runs as a fully trusted application and behaves similar to a desktop windows application. mshta.exe is used to run the HTML application. When we click this shortcut we will run mshta.exe and pass html file as the argument to this.
Note:When running HTAs, users should take the same precautions as with any executable: Only install HTAs produced by reliable sources
6. Double click on the shortcut and you should see running Silverlight desktop application as shown below!
8. We can create a setup project to package Silverlight application and all the end users have to do is just use the installer to install it, just like any standard desktop windows application.
Note that only files you need to include in the setup package are xap file and host HTML file.
You can download the code for this sample from here.
Your comments on this are appreciated.


Comments
Pretty slick, but I think you are missing the raison d'être for Silverlight on the desktop, which is to build cross platform apps. Your solution would require IE to be installed on other plaforms.
Posted by: snort | February 9, 2009 06:03 PM