Microsoft MSDN:
Http://msdn.microsoft.com/zh-cn/library/cc656722 (v = vs.90). aspx
This example demonstrates the following:
Download sample
Carrying Silverlight-based applications in XBAP
You can use the WebBrowser control to host Silverlight-based applications in XBAP. The content of XBAP Page contains the WebBrowser control. The source URI of this control can identify HTML pages that carry Silverlight-based applications. The HTML page uses the Silverlight plug-in control to host Silverlight-based applications.
This section describes the High-level architecture of a Silverlight-based application in XBAP.
Communication from XBAP to Silverlight-based applications
The host XBAP can communicate with the Silverlight-based Host application using the script method. The script method here is implemented by carrying the HTML page of The Silverlight-based application. XBAP uses the InvokeScript method to call the script method. The script method then calls the method implemented by the Scriptable object. The object is made public by a Silverlight-based application, and its script call can be implemented through the Content attribute of the Silverlight plug-in control.
This section describes the High-Level Architecture of Silverlight-based applications in XBAP and the key technologies used to call the method for transmitting methods from the host xbap to the Silverlight-based Host application.
To use the WebBrowser control to call the script method, see the WebBrowser control example. For more information about implementing Silverlight-based applications (used to publish Scriptable objects), see Walkthrough: Calling Managed Code from JavaScript (Walkthrough: Calling Managed Code from JavaScript ). For more information about how to develop Silverlight-based applications, see Getting Started with Silverlight ).
Communication from Silverlight-based applications to XBAP
The Silverlight-based Host application can communicate with the host XBAP by using the script method. The script method here is implemented by carrying the HTML page of The Silverlight-based application. Silverlight-based applications use the Invoke method of the HtmlWindow class (from the System. Windows. Browser namespace) to call the script method. A Silverlight-based application obtains a reference to a Window (represented by the HtmlWindow class) from the Window attribute of the HtmlPage class (also located in the System. Windows. Browser namespace. The script method then calls the method implemented by an object that can write scripts. The object is made public from XBAP through the ObjectForScripting attribute, and its script call can be made public through the window object (published by the host browser).
This section describes the High-level architecture of the Silverlight-based application in XBAP, and the key technologies used to call the Silverlight-based Host application to the host XBAP propagation method.
To call the script method from a Silverlight-based application, see Walkthrough: Calling JavaScript from Managed Code (Walkthrough: Calling JavaScript from Managed Code ). For more information about publishing Scriptable objects from XBAP, see the WebBrowser control example and ObjectForScripting.
Configuration example
This example consists of a solution (XBAPHostingSilverlightSample) and two projects:
XBAP Project (WPFBrowserApplication) is used to host Silverlight-based applications.
Silverlight-based application project (SilverlightApplication ).
SilverlightApplication is generated after WPFBrowserApplication. It includes a post-generation step. The generated Silverlight-based application silverlightspplication. xap is copied to the output folder of the WPFBrowserApplication project: \ bin \ debug.
You must use the following steps to configure the example so that it directs to the virtual directory of the WPFBrowserApplication project Output Folder (\ bin \ debug) from localhost to run:
Select "Internet Information Service (IIS) manager" from "start" | "Administrative Tools ".
Select "website" | "default website ".
Right-click "default website", select "add virtual directory", and then
Set "alias" to "WPFBrowserApplication ".
Set "physical path" to the output folder of the wpfbrowserapplication project.
Click "OK" to create a virtual directory.
Running example
To run the example, navigate to the following URL:
Http: // localhost/WPFBrowserApplication. xbap
Best practices
This example demonstrates specific features of the Windows Presentation Foundation and does not follow best practices for application development. For a comprehensive introduction to best practices for Windows Presentation Foundation (WPF) and Microsoft. NET Framework application development, see the following topics:
Auxiliary functions-optimal solution for auxiliary functions
Localization-WPF overview of globalization and Localization
Performance-optimizes the performance of WPF applications
Security-Windows Presentation Foundation Security
Generation example
Install the Windows software development kit (SDK) and open its generate Environment command window. On the Start Menu, point to all programs and Microsoft Windows SDK, and then click CMD Shell ".
Download the example (usually in the software development kit (SDK) Documentation) to your hard disk.
To generate an example from the generate Environment command window, go to the source directory of the example. At the command prompt, type MSBUILD.
To generate an example in Microsoft Visual Studio, load the sample solution or project file, and press Ctrl + Shift + B.
Running example
To run compiled examples in the Command window of the Generation Environment, run the. exe file in the Bin \ Debug or Bin \ Release folder in the sample source code folder.
To debug compiled examples in Visual Studio, press F5.