SilverLight 3.0 Out of Browser Mini Tutorial

SilverLight 3.0 has just been released to the web. And there is now an even more important reason to start using this great technology – running apps written for the browser can now be run as a standalone application from the user’s desktop as well. This post takes a detailed look at how you can start using this new feature.

The first thing of course is to the install the SilverLight 3.0 runtime, Tools for Visual Studio 2008 and the SilverLight Toolkit which adds a number of new controls. Now fire up Visual Studio 2008 or Visual Web Developer 2008 (both going to be called VS in this post) and create a new SilverLight Application project. When prompted to create a Web project, accept the defaults and go on.

Now you can go ahead and create the SilverLight project as you normally would. (Please note that teaching the basics of SilverLight is beyond the scope of this post, but there are many great tutorials out there.) Once this has been done, test out your application and see if it works in the browser.

To add support for the Out-of-Browser (OOB) experience, simply right-click the SilverLight project in Solution Explorer and go to Properties. Simply turn the Enable running applications out of the browser option On. SL1

 

You can also click the button next to it to customize things like the window title, the name of the shortcut, the window size, icons and more.SL2

Please note: A lot of tutorials made for the SL3 beta ask you to change the AppManifest.xml file and add a section to it called <Deployment.AppSettings>. This will not work in the final release of SilverLight 3.0. The procedure above creates a new OutOfBrowserSettings.xml in project that has a different XML schema.

Once this is done, you can view the app in the browser as usual. However, now on right-clicking inside the app, you will see this menu. SL3

Clicking the menu will allow you to install the application on your computer with a shortcut on the desktop or Start menu through this dialog that comes up:SL4

Now simply click the icon on your desktop or Start menu to load the application in its own standalone window. You can even right-click in the window to uninstall the application from your system.SL5

This allows you to have a desktop client version of the application ready for usage anytime rather than having to go to the site every time to use it.

Advanced OOB

There are many other things you can do with the OOB experience as well. I’ll take a look at just two of them – creating a custom install action in your application and checking and installing updates automatically.

Custom Install Action

Figuring out that the application can be installed might not be intuitive for an end user. So you might want to give a simple way for the user to initiate this – say by using a button or link. For instance, in the above application, I’ve added a HyperLinkButton control below the Calculate button. I also detect whether the app is already installed or not before showing this button. The code to do this is shown below.

public partial class MainPage : UserControl
    {
        Application app = Application.Current;

        public MainPage()
        {
            InitializeComponent();
            hlInstall.Visibility = 
              (app.InstallState == InstallState.Installed ? 
              Visibility.Collapsed: Visibility.Visible);

The above code creates a reference to the current application and then turns the visibility of the hyperlink off or on depending on whether the application is installed locally or not. Clicking the link brings up the same dialog as before.SL6

Updating the Application

The great part about the OOB SilverLight application is that if there is an update on the server and the user is running it locally, the application can download and install the new version automatically. This is fairly simple to do as well. You simply need to call a function to check and update. Take a look at the code below.

        public MainPage()
        {
            InitializeComponent();
            hlInstall.Visibility = 
                (app.InstallState == InstallState.Installed ? 
                Visibility.Collapsed: Visibility.Visible);
            app.CheckAndDownloadUpdateCompleted += 
                new CheckAndDownloadUpdateCompletedEventHandler
                (app_CheckAndDownloadUpdateCompleted);
            app.CheckAndDownloadUpdateAsync();
        }

        void app_CheckAndDownloadUpdateCompleted(
            object sender, 
            CheckAndDownloadUpdateCompletedEventArgs e)
        {
            if (e.UpdateAvailable)
            {
                ChildWindow c = new ChildWindow();
                c.Title = "Update";
                c.Content = 
                    "A new version of this application 
                    has been downloaded & installed. \n\n
                    Please restart the application to see the changes.";
                c.Show();
            }
        }

When the app initializes, it starts an async check for an update and if it exists it downloads it. The async return event then informs the user that the app has been updated and to restart it to see the changes. SL7

As you can see, the OOB experience lets you create applications and deploy them very easily to client machines. It also allows automatic updates to the application without user intervention easily as well.

There are a number of other things in the OOB feature that I haven’t touched upon in this post – such as network connectivity detection, offline and online support and more. Check out the SilverLight 3.0 documentation to see more about these features. If you are interested in the code for the above project, you can download the entire solution here.

Hope you have fun with SilverLight 3.0. I think this is a great tool going forward to create small apps that can run of the Web as well as locally installed. i think a lot of Government agencies can use this technology to deliver projects such as Income Tax return, Service Tax filings and more created in Silverlight. End users can use the Web version, whereas tax consultants and chartered accountants can install it locally. Anytime the tax rates or form definitions change, the government can simply change it in the Web version and have it updated on all users’ systems as well automatically.


Tags: , ,
Categories: Development | SilverLight

5 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Comments

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

July 21. 2009 13:05 | sulumits retsambew United States |

Thank you very much for sharing this post.

July 30. 2009 22:55 | laser pointers United States |

I already digg this post.. I like it, it is very interesting to me.

August 3. 2009 14:18 | moratmarit United States |

Yeah indeed pretty good post. The post is going to be a great help or tip for developer who are using Silverlight 3.0. Very nice, thanks for sharing.  

August 5. 2009 10:09 | Best weight loss pills United States |

thanks for the tutorials.

August 5. 2009 18:42 | Stop Dreaming Start Action United States |

Comments are closed