Friday, June 27, 2008

Making Silverlight Run As an EXE

well sort of.

So I've seen a few things like this: http://www.codeplex.com/SilverlightDesktop and thought I would see how easy it was... turns out to be 'super' simple...

Silverlight is designed to work inside a web browser. That being the case it is also not a ‘desktop’ or exe application. This ‘hack’ then allows us to take Silverlight applications and make them into exe’s. It works great however there are a few issues to keep in mind. One issue is that as shown here the exe won’t run on a 64 bit machine unless you get the 32bit IE engine running in the context of the control we will show. Also Silverlight still needs to be installed on the box and the one small detail about this only working on a windows machine but otherwise works well. The other major down sides is that a lot of things dependent on script injection or cross domain issues will in fact be ‘issues’ but you can work around that.

To start we just need to create a new project in Visual Studio. From the new project window we want to select a Windows Forms Application under windows (under the language of your choice). Once we do this we should get a blank Form 1. You can change the setting or set other values as you like. To get our Silverlight application running we need to add a folder that contains an HTML page and XAP that works from any local path. For example in our first project we could copy the output from our debug folder and call it good.

Once you drag a Silverlight application folder into our project we need to select on each item such as the HTML page and XAP files. In the properties pain of Visual Studio for each item there is a setting called ‘Copy to Output Directory.’ We need to change this setting for everything our Silverlight application depends on to ‘Copy always’.

Now that all the resources are present we need to start adding to the ‘Form’. Basically what we are going to do is add a web browser control that hosts the IE rendering engine that will load our Silverlight application in a web page running locally in an exe. In the tool box on the left side of visual studio under the tap ‘Common Controls’ drag the control onto the form on the design surface. Then resize the form and set any other properties you might want to set (ie: add an icon, or change title etc).

Right click on the form and select ‘View Code’ and add the following code:

string MyPath = Application.StartupPath + "\\[folder of Silverlight App]\\[Some Page].html";

webBrowser1.Navigate(MyPath);

The first line gets the path to our page that has the code to load the XAP for our application. Now then we pass this value into a call to the Navigate method on the web browser control. If you rename it the control name will be different but the sample above is the default. Compile and run the application and you will get your Silverlight application running in a win 32 window. If you wanted to be industrious you get build this in a VB 6 or C++ app doing the same kind of thing and even embed all the resources into the exe or build a custom installer etc and have this running on just about any windows platform with Silverlight even without .NET.