Using the WebBrowser control and debug a Silverlight Out-of-browser (OOB) application in VS 2010

Yesterday I played with the WebBrowser control shipped with Silverlight 4. I was trying to create a little RSS Reader for Silverlight 4, only to try out some of the new features. The WebBrowser control have two methods to display a web page, the Navigate and the NavigateToString. The Navigate takes an URI, the NavigateToString takes a string with for example HTML to be diplsyed inside of the WebBrowser window. In my case I didn’t want to use the Navigate, instead the NavigateToString so I could just grab some RSS feeds and get the body of a post and pass it to the WebBrowser control. First of all I notice that the WebBrowser control will only work in a Out-of-browser application, if we try to use it in a browser we will se the following:

image

Here is the XAML:

<UserControl x:Class="SilverlightBlog.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <WebBrowser x:Name="MyBrowserControl" Width="800" Height="600" />
    </Grid>
</UserControl>

To enable out-of-browser we can right click on the Silverlight application and select properties in the menu, then under Silverlight, we have the “Enable running application out of browser” option, and also the button “Out-of-Browser Settings”, to set up the out-of-browser application.

image

Note: I have selected the “Require elevated trust when running outside the browser” checkbox. The reason to this is to be able to use the WebClient class or the WebBrowser control to navigate to any URL. If the checkbox is not selected, we can only open pages located on our own server.

With the “Require elevated trust when running outside the browser”, I can now easy use the WebClient to request a URL anywhere in the cloud:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

        if (App.Current.IsRunningOutOfBrowser)
        {
           var blogRequest = new WebClient();
           blogRequest.DownloadStringCompleted += blogRequest_DownloadStringCompleted;
           blogRequest.DownloadStringAsync(
                       new Uri("http://weblogs.asp.net/fredriknormen/rss.aspx", UriKind.Absolute));
        }
    }

    void blogRequest_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        MyBrowserControl.NavigateToString(e.Result);
    }
}

In the code I will first check if the application is currently running out-of-browser. It not, there are no reason to try to request a URL and then add the response to the WebBrowser control. As you can see I take the RSS returned from my blog and added it to the NavigateToString method of the WebBrowser. This will only show a lot of text. I only wanted to see if I could get my RSS feed. What I was planning to do is to use the XmlReader to only get the blog post content which includes HTML and pass it to the NavigateToString method. During the implementation of this example, I wanted to debug the out-of browser application. One way to do it is by attaching to the out-of-browser launcher, but there is another way we can do it in VS 2010. First we need to install our application as out-of-browser:

image

image

Note: When running in a elevated trust we will get another installation window. You will also notice that we now have a timer on the Install button when we install a out-of-browser app. So we need to wait about 3 seconds before we can install an app. It will make sure people need to pay more attention to what the window are displaying.

Now when the application is up an running we can close it, then go back to VS 2010 and go to the property window for our Silverlight project, select the Debug tab and the “Installed out-of-browser application”. Make sure your Silverlight project is not the start-up project. Then just start debugging. I love this feature!

image


If you want to know when I publish more posts to my blog, you can follow me on twitter: http://www.twitter.com/fredrikn

3 Comments

Comments have been disabled for this content.