Troubleshooting Silverlight Out-of-Browser Problems

It is quite easy to enable Out-of-Browser (OOB) mode when building a Silverlight application.  Open up the project properties on the Silverlight project and check the box to "Enable running application out of the browser."

The more difficult task is troubleshooting problems when the application works in browser, but not out of browser.  First, to debug OOB applications in Visual Studio, you need to open the Silverlight project properties and in the "Debug" tab, set the start action to "Out-of-browser application" and select the appropriate project from the dropdown list.  Next, set your Silverlight project as the startup project.  Now when you debug in Visual Studio, it will open the application in an OOB window instead of the default browser.

Even when you have debugging set up, some OOB problems are still difficult to track down.  There are several things that will give you a blank screen in the OOB window even though no exceptions are displayed in Visual Studio.  Here are some common problems to check:

HtmlPage References

You can reference the System.Windows.Browser.HtmlPage class when running the application within a browser, but in OOB mode, it will throw an exception.  And even though you may have the debugger attached, this typically results in a blank screen depending on where the HtmlPage class is used.  If you do need to use HtmlPage, wrap it in a check for OOB mode.  Here is an example of code I typically have in App.xaml.cs to set focus on the Silverlight plugin within a browser window:

// Focus the Silverlight plugin, but only when running in-browser
if (!Application.Current.IsRunningOutOfBrowser)
{
    HtmlPage.Plugin.Focus();
}

Without the IsRunningOutOfBrowser check, this would throw an exception in OOB mode.


InitializeRootVisual and WCF calls

If you are using WCF calls or WCF RIA Services, there is another elusive problem that can arise.  While there is no problem when running in browser, OOB mode has a problem with making a WCF call before the RootVisual is set.  So if you are calling a method like LoadUser(), be sure to call InitializeRootVisual() first:

private void Application_Startup(object sender, StartupEventArgs e)
{
    // This will enable you to bind controls in XAML files to WebContext.Current properties
    this.Resources.Add("WebContext", WebContext.Current);
 
    // Initialize the RootVisual before LoadUser to prevent OOB problems
    InitializeRootVisual();
 
    // This will automatically authenticate a user when using windows authentication
    // or when the user chose "Keep me signed in" on a previous login attempt
    WebContext.Current.Authentication.LoadUser(Application_UserLoaded, null);
}

Are there any other tricks you've found to resolve Out of Browser problems?

comments powered by Disqus