Moving Beyond the Browser – Sandboxed Applications
Out-of-Browser Windowing Updates
Silverlight 4 has some new Out-of-Browser features including the ability to resize the window programmatically, position the window to start up anywhere on the screen, and set the window’s z-order. You can also set the window state to maximized or minimized.
Hosting Web Content within Silverlight Applications
The WebBrowser control allows you to display HTML in the control when running Out-of-Browser. When you use a WebBrowser control in an application that runs inside a browser, a rectangle the same size as the WebBrowser control will display instead of the WebBrowser and its content. There are three ways to load web content inside of the WebBrowser control:
Silverlight adds a new type of brush called an WebBrowserBrush that accepts a WebBrowser control as its source. The SetSource method accepts a WebBrowser control and displays the content using the WebBrowserBrush in another UI Element. The WebBrowserBrush is a brush which means it is non-interactive HTML. For example, a Path, Ellipse, or Rectangle can be painted using the WebBrowserBrush, which gets its source from the www.Microsoft.com Web page. The XAML below defines a Path with an WebBrowserBrush.
You can set the WebBrowserBrush to a WebBrowser control that loads the www.microsoft.com Web page, as shown in the code below.
Painting a Path with an WebBrowserBrush
Now Silverlight’s Out-of-Browser applications can use the NotificationWindow class to display notifications (also known as toasts). Notifications let you alert the user that a significant event has occurred in the Out-of-Browser application. You can create a new instance of the NotificationWindow, set its height and width, then set its Content property to some XAML content. Finally, to display the notification window, invoke the Show method and pass to it the number of milliseconds it should be displayed, as shown in the sample code below.
The previous code shows a simple example that displays a basic notification. However, you can also customize the content for a notification to look any way you want. The image below shows a customized notification.
Window Closing Event
Silverlight 4 adds the ability to discover when an out of browser application’s window is being closed. The Closing event offers the opportunity to cancel this operation (unless it is being closed due to the user shutting down or logging off from the system). The following code could be added to handle the Closing event.
The handler for the Closing event (shown below) checks if a CheckBox named canCloseCheckBox is checked. If so and the application is being closed by the user (not by a user shut down or logging off the system), the window is then closed.