下面的示例代码演示如何从主机服务器按需检索程序集,然后将其加载到当前应用程序域中。
此示例使用 WebClient 类启动程序集的异步下载以响应用户鼠标单击。当下载完成后,将使用 AssemblyPart 类来加载此程序集。
此示例假定您已在您的应用程序项目中添加了对程序集的引用。该程序集的"复制本地"值还必须为"False",这样,它将不会部署在应用程序包中。有关更多信息,请参见应用程序结构。
XAML
复制代码
Page from Host Application Click Here to display a UI from the Library Assembly
Visual Basic
复制代码
Imports SilverlightLibrary Partial Public Class HomePage Inherits UserControl Public Sub New() InitializeComponent() End Sub Private Sub TextBlock_MouseLeftButtonUp(ByVal sender As Object,_ ByVal e As MouseButtonEventArgs) ' Download an "on-demand" assembly. Dim wc As New WebClient() AddHandler wc.OpenReadCompleted,AddressOf wc_OpenReadCompleted wc.OpenReadAsync(New Uri("SilverlightLibrary.dll",UriKind.Relative)) End Sub Private Sub wc_OpenReadCompleted(ByVal sender As Object,_ ByVal e As OpenReadCompletedEventArgs) If (e.[Error] Is nothing) AndAlso (e.Cancelled = False) Then ' Convert the downloaded stream into an assembly that is ' loaded into current AppDomain. Dim assemblyPart As New AssemblyPart() assemblyPart.Load(e.Result) displayPageFromLibraryAssembly() End If End Sub Private Sub displayPageFromLibraryAssembly() ' Create an instance of the Page class in the library assembly. Dim page As New Page() ' display the new Page. Me.stackPanel.Children.Add(page) End Sub End Class
C#
复制代码
using System; // Uri using System.Net; // WebClient,OpenReadCompletedEventHandler using System.Windows; // AssemblyPart using System.Windows.Controls; // UserControl using System.Windows.Input; // MouseButtonEventArgs using SilverlightLibrary; // Page namespace SilverlightApplication { public partial class HomePage : UserControl { public HomePage() { InitializeComponent(); } private void TextBlock_MouseLeftButtonUp( object sender,MouseButtonEventArgs e) { // Download an "on-demand" assembly. WebClient wc = new WebClient(); wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted); wc.OpenReadAsync( new Uri("SilverlightLibrary.dll",UriKind.Relative)); } private void wc_OpenReadCompleted( object sender,OpenReadCompletedEventArgs e) { if ((e.Error == null) && (e.Cancelled == false)) { // Convert the downloaded stream into an assembly that is // loaded into current AppDomain. AssemblyPart assemblyPart = new AssemblyPart(); assemblyPart.Load(e.Result); displayPageFromLibraryAssembly(); } } private void displayPageFromLibraryAssembly() { // Create an instance of the Page class in the library assembly. Page page = new Page(); // display the new Page. this.stackPanel.Children.Add(page); } } }
若要查看该示例的运行版本,请单击下面的链接。
参考
其他资源
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。