Join Walt Ritscher for an in-depth discussion in this video Creating an Out-of-Browser application, part of Silverlight 4 New Features.
Up until very recently, there were two models for building applications. The desktop model's a familiar one; re-create an application that runs on Windows or the Mac OS. This application is installed locally, and can exploit the rich services offered by the operating system. Then there is the web model. This model has rocketed to the top of the food chain in the last ten years. Web apps are launched from the browser, run within the browser window and are often limited in what they can do with the local computer. But more and more, we are seeing a blurring of the lines in web applications.
Adobe AIR is an example of a technology that allows apps to run in or out of the browser. And now, of course, Silverlight supports the idea of an out-of-browser application. This is often referred to as an OOB app. So what are the benefits of using OOB? First, the application runs in its own window, not a browser window, and in Silverlight, it is hosted into process called SL Launcher. As you might guess from the name, an OOB application does not require an Internet connection.
The app launches locally and runs like any other local application. The application is automatically updatable, however. If an Internet connection is available, then the application checks with the server to see if the new version is available. If so, the new version is always automatically downloaded. The next time you run that application, it uses the new version. It's time to show you how to create your own out-of-browser Silverlight application. As you can see, I have Visual Studio 2010 open on my desktop, and I've opened this project called CreatingOobApp.
When I run this application by pressing F5, it will launch a browser window and load my Silverlight application in that browser window. When I right-click on this, you'll see a context menu that says Silverlight. This is not yet an OOB application. When I return back to Visual Studio, all I need to do to convert this to an OOB application is double-click on the Properties node in the Solution Explorer and then choose Enable running application out of the browser.
Now, when I run the application, I no longer see a browser window. And when I right-click on the application, I see a Remove this application. Now that seems odd that I see that Remove this application, but that's because the default setting in Visual Studio is to automatically install the application when I debug it. If I would rather see the install menu, I need to go back to Visual Studio, choose Debug, and uncheck this Out-of- browser application and instead choose Dynamically generate a test page.
Let's run the application again and see what it looks like. It looks the same. If I right-click, I now see Install Creating OOB Application onto this computer. When you choose this option, you are asked if you like a shortcut on your Start menu and your desktop. I think that's a great idea, so I'm going to choose both and then click OK. Now, the application is running out of the browser, and I also have, in my Start menu, a new shortcut for creating the OOB application.
If I click on this shortcut, it launches another instance of my out-of-browser application. So now I have two versions of the out-of-browser and one running inside my Firefox browser. When your user tires of the application, all they need to do is right-click and choose remove this application. Now, all traces of that application are gone from this machine. Alternatively, you can go to your Control Panel, usually in the Programs and Features section and choose to uninstall it from there.
For more control over my application, what I can do is go into my Properties window, choose Silverlight, and then go into this Out-of-Browser Settings. Here, I can change what the text is actually shown on the shortcut. Also, you might want to control how the user installs; rather than having the user right-click, maybe you would like to put your own Install button in your application. Let me show you how to do that. I'm going to open this file called CheckOob.xaml, and then I'm going to make it my start page by going to my App.xaml.cs file, double-click, and I'm going to edit Line 29 here, where it says I want to instantiate the MainPage.
Now, I want to instantiate the CheckOob page. Press F5 to run the application, and here is our Start page. Notice that it says Is Network Available is true, the Internet is available, and I am currently running this in the browser. When I click on this button, I see the same Install dialog we saw a few minutes ago. And now, take a look at the difference on the screens. This says the Internet is available, and it also says the app is running out of browser.
So we can detect not only is the Internet connection available, but is the application running in or out of the browser? Let me show you how the code does that. Switching back to Visual Studio, opening up my CheckOob.xaml.cs file by double-clicking, and the installation code is simple. It's one line of code down here at the bottom: Application.Current.Install(). That's how simple it is to install an out-of-browser application. It's almost as simple to find out if the network is available.
There is a function called GetIsNetworkAvailable(), which I can call to find out if I'm on the Internet. This is important because if you're trying to call a service on the web, say, to download a music file, you need to know if you're running out of browser, and if the network is available for you to do that call. I can also find out if I'm in the browser by checking this Boolean property: IsRunningOutofBrowser. So what I've shown you so far is how to create a basic out-of-browser application.
In other movies in this chapter, you'll learn more about configuring these kinds of applications.
Recommended prerequisite: Silverlight 3 Essential Training.
- Alerting users with the Notifications window
- Creating elevated trust applications
- Signing with XAP
- Simplifying binding with the property marker
- Analyzing data with PivotViewer
- Localizing content
- Working with design-time data in Expression Blend
- Adding interactivity with Behaviors