XBAP Hosting Silverlight Sample
Updated: July 2008
This sample demonstrates the following:
How to host a Silverlight-based application in a XAML browser application (XBAP).
How to communicate between the host XBAP and the hosted Silverlight-based application.
Silverlight-based applications can be hosted in XBAPs by using the WebBrowser control. The content of a XBAP Page contains the WebBrowser control whose source URI identifies the HTML page that hosts the Silverlight-based application. The HTML page uses the Silverlight plug-in control to host the Silverlight-based application.
The following image illustrates the high-level architecture for hosting a Silverlight-based application in an XBAP.
A host XBAP can communicate with a hosted Silverlight-based application via script methods implemented by the HTML page that hosts the Silverlight-based application. An XBAP calls the script methods using the InvokeScript method. The script methods then call methods implemented by a scriptable object that is exposed from the Silverlight-based application and available to script from the Content property of the Silverlight plug-in control.
The following figure shows both the high-level architecture for hosting a Silverlight-based application in an XBAP and the key technology used to propagate method calls from a host XBAP to a hosted Silverlight-based application.
A hosted Silverlight-based application can communicate with a host XBAP via script methods implemented by the HTML page that hosts the Silverlight-based application. A Silverlight-based application calls the script methods using the Invoke method of the HtmlWindow class (from the Silverlight System.Windows.Browser namespace). A Silverlight-based application gets a reference to the window (represented by the HtmlWindow class) from the Window property of the HtmlPage class (also in the System.Windows.Browser namespace). The script methods then call methods implemented by a scriptable object that is exposed by from the XBAP, using the ObjectForScripting property, and available to script from the external property of the window object that is exposed by the host browser.
The following figure shows both the high-level architecture for hosting a Silverlight-based application in an XBAP and the key technology used to propagate method calls from a hosted Silverlight-based application to a host XBAP.
The sample consists of one solution (XBAPHostingSilverlightSample) with two projects:
The XBAP project (WPFBrowserApplication) that hosts the Silverlight-based application.
The Silverlight-based application project (SilverlightApplication).
SilverlightApplication builds afterWPFBrowserApplication and includes a post-build step that copies the generated Silverlight-based application, silverlightspplication.xap to the WPFBrowserApplication project output folder: \bin\debug.
The sample must be configured to run on the localhost from a virtual directory that points to the WPFBrowserApplication project output folder (\bin\debug) using the following steps:
From Start | Administrative Tools, choose Internet Information Services (IIS) Manager.
Select Web Sites | Default Web Site.
Right-click Default Web Site and choose Add Virtual Directory, then
Set the Alias to "WPFBrowserApplication".
Set the Physical path to the WPFBrowserApplication project output folder.
Press OK to create the virtual directory.
This sample demonstrates a specific feature of the Windows Presentation Foundation and, consequently, does not follow application development best practices. For comprehensive coverage of Windows Presentation Foundation (WPF) and Microsoft .NET Framework application development best practices, refer to the following as appropriate:
Accessibility - Accessibility Best Practices
Localization - WPF Globalization and Localization Overview
Performance - Optimizing WPF Application Performance
Security - Windows Presentation Foundation Security
Install the Windows Software Development Kit (SDK) and open its build environment command window. On the Start menu, point to All Programs, Microsoft Windows SDK, and then click CMD Shell.
Download the sample, usually from the software development kit (SDK) documentation, to your hard disk drive.
To build the sample from the build environment command window, go to the source directory of the sample. At the command prompt, type MSBUILD.
To build the sample in Microsoft Visual Studio, load the sample solution or project file and then press CTRL+SHIFT+B.