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:

  1. The NavigateToString method
  2. The Navigate method
  3. The Source property

    The NavigateToString method accepts a string that represents content (HTML and JavaScript). The content is passed to this method is rendered in the WebBrowser control. The Navigate method accepts a URI that points to the HTML content that you want to render. The URI does not need to be in the same domain as the Silverlight application. The Source property is another way to set the content to a URI.

    The following code loads the WebBrowser control wb with HTML content when run Out-of-Browser.


    string content = "<table border=3 cellpadding=3><tr><td><font size=36" + "color='red'>Red</font></td><td><font size=36" + "color='blue'>Blue</font></td></tr></table>"; wb.NavigateToString(content)

    Figure 27

    Embedded Web Content

    The following code loads the Web page in the WebBrowser control when run Out-of-Browser.


    wb.Navigate(new Uri(""));

    Figure 28

    Loading in a 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 Web page. The XAML below defines a Path with an WebBrowserBrush.


<Path Data="M0.5,0.5 L68.247093,0.5 L67.29586,1.0469482 C60.223965,5.343935 55.5,13.120297 55.5,22.000002 C55.5,35.530975 66.469025,46.5 80,46.5 C93.530975,46.5 104.5,35.530975 104.5,22.000002 C104.5,13.120297 99.776031,5.343935 92.70414,1.0469482 L91.752907,0.5 L159.5,0.5 L159.5,57.298336 L159.91597,56.378239 C163.87273,48.166615 172.27461,42.5 182,42.5 C195.53098,42.5 206.5,53.469025 206.5,67 C206.5,80.530975 195.53098,91.5 182,91.5 C172.27461,91.5 163.87273,85.833389 159.91597,77.621765 L159.5,76.70166 L159.5,133.5 L89.70166,133.5 L90.621765,133.91597 C98.833389,137.87274 104.5,146.27461 104.5,156 C104.5,169.53098 93.530975,180.5 80,180.5 C66.469025,180.5 55.5,169.53098 55.5,156 C55.5,146.27461 61.166618,137.87274 69.378235,133.91597 L70.298332,133.5 L0.5,133.5 L0.5,78.752907 L1.0469483,79.70414 C5.343935,86.776031 13.120296,91.5 22.000002,91.5 C35.530975,91.5 46.5,80.530975 46.5,67 C46.5,53.469025 35.530975,42.5 22.000002,42.5 C13.120296,42.5 5.343935,47.223965 1.0469483,54.29586 L0.5,55.247093 z" Stroke="#FF666666" Stretch="Fill" StrokeThickness="0.5" StrokeLineJoin="Round"> <Path.Fill> <WebBrowserBrush x:Name="WebBrowserBrush" AlignmentX="Left" AlignmentY="Top" Stretch="None"/> </Path.Fill> </Path>

You can set the WebBrowserBrush to a WebBrowser control that loads the Web page, as shown in the code below.



Figure 29

Painting a Path with an WebBrowserBrush

Notifications (Toast)

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.


NotificationWindow notify = new NotificationWindow(); notify.Width = 330; notify.Height = 75; TextBlock tb = new TextBlock(); tb.Text = "Sample notification"; tb.FontSize = 24; notify.Content = tb; notify.Show(3000);

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.

Figure 30

Custom 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.


Window mainWindow = Application.Current.MainWindow; void SomeMethod() { if (Application.Current.IsRunningOutOfBrowser) { mainWindow.Closing += MainWindow_Closing; } }

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.


void MainWindow_Closing(object sender, System.ComponentModel.ClosingEventArgs e) { if (canCloseCheckBox.IsChecked == false && e.IsCancelable) { e.Cancel = true; } }