Silverlight如何按需加载程序集

下面的示例代码演示如何从主机服务器按需检索程序集,然后将其加载到当前应用程序域中。

此示例使用 WebClient 类启动程序集的异步下载以响应用户鼠标单击。当下载完成后,将使用 AssemblyPart 类来加载此程序集。

此示例假定您已在您的应用程序项目中添加了对程序集的引用。该程序集的"复制本地"值还必须为"False",这样,它将不会部署在应用程序包中。有关更多信息,请参见应用程序结构

clear.gif 示例

XAML

复制代码

<usercontrol x:class="SilverlightApplication.HomePage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><grid background="SandyBrown"><stackpanel x:name="stackPanel"><textblock>Page from Host Application</textblock><textblock mouseleftbuttonup="TextBlock_MouseLeftButtonUp" cursor="Hand">
                Click Here to Display a UI from the Library Assembly
            </textblock></stackpanel></grid></usercontrol>

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);
        }
    }
}

若要查看该示例的运行版本,请单击下面的链接。

运行此示例

clear.gif 请参见

参考

WebClient

AssemblyPart

其他资源

应用程序结构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值