Silverlight如何按需加载程序集

news/2024/7/4 1:09:44 标签: silverlight, assembly, library, object, download, class
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

 

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

此示例使用 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" class="tags" href="/tags/ASSEMBLY.html" title=assembly>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 class="tags" href="/tags/DOWNLOAD.html" title=download>downloaded stream into an class="tags" href="/tags/ASSEMBLY.html" title=assembly>assembly that is 
            ' loaded into current AppDomain. 
            Dim class="tags" href="/tags/ASSEMBLY.html" title=assembly>assemblyPart As New AssemblyPart()
            class="tags" href="/tags/ASSEMBLY.html" title=assembly>assemblyPart.Load(e.Result)

            DisplayPageFromLibraryAssembly()

        End If

    End Sub

    Private Sub DisplayPageFromLibraryAssembly()

        ' Create an instance of the Page class in the class="tags" href="/tags/LIBRARY.html" title=library>library class="tags" href="/tags/ASSEMBLY.html" title=assembly>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(
            class="tags" href="/tags/OBJECT.html" title=object>object sender, MouseButtonEventArgs e)
        {
            // Download an "on-demand" class="tags" href="/tags/ASSEMBLY.html" title=assembly>assembly.
            WebClient wc = new WebClient();
            wc.OpenReadCompleted += 
                new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(
                new Uri("SilverlightLibrary.dll", UriKind.Relative));
        }

        private void wc_OpenReadCompleted(
            class="tags" href="/tags/OBJECT.html" title=object>object sender, OpenReadCompletedEventArgs e)
        {
            if ((e.Error == null) && (e.Cancelled == false))
            {
                // Convert the class="tags" href="/tags/DOWNLOAD.html" title=download>downloaded stream into an class="tags" href="/tags/ASSEMBLY.html" title=assembly>assembly that is
                // loaded into current AppDomain.
                AssemblyPart class="tags" href="/tags/ASSEMBLY.html" title=assembly>assemblyPart = new AssemblyPart();
                class="tags" href="/tags/ASSEMBLY.html" title=assembly>assemblyPart.Load(e.Result);

                DisplayPageFromLibraryAssembly();
            }
        }

        private void DisplayPageFromLibraryAssembly() {

            // Create an instance of the Page class in the class="tags" href="/tags/LIBRARY.html" title=library>library class="tags" href="/tags/ASSEMBLY.html" title=assembly>assembly.
            Page page = new Page();

            // Display the new Page. 
            this.stackPanel.Children.Add(page);
        }
    }
}

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

运行此示例

  请参见

参考

WebClient

AssemblyPart

其他资源

应用程序结构


http://www.niftyadmin.cn/n/1706202.html

相关文章

VC++ MFC DLL模板

1、VS2003新建DLL项目dllTest 2、项目dllTest中添加类CDllDailog 3、切换到Resource view界面,添加新Button(ID:IDC_BUTTON_Hello Caption:Hello),并点击进入DllDialog.cpp脚本,添加代码如下(红色): 1 #include "…

Silverlight应用程序扩展服务

使用应用程序扩展服务可以扩展 Silverlight 应用程序模型。通常使用扩展服务来封装特定功能域中由多个应用程序使用的功能。例如,可以使用扩展服务来实现专用媒体处理或自定义数据访问层。 Application 类提供多数应用程序共用的服务。有关更多信息,请参…

新疆计算机考试ppt教程,2014新疆维吾尔自治区计算机等级考试二级考试答题技巧...

2014新疆维吾尔自治区计算机等级考试二级考试答题技巧1、在内存中,每个基本单位都被赋予一个唯一的序号,这个序号称为( C )A、字节 B、编号 C、地址 D、容量2、我国颁布汉字编码标准GB 2313-1980,是规定了汉字的( B )A、内部码 B、交换码 C、…

class$1,class$2,class$innerclass中的$的含义

class文件名中的$的含义如下: $后面的类是$前面的类的内部类 内部类有以下两种情况: 1.普通的组合类形式,即在一个类内部定义一个普通的类 public class Outer {class Inner {public Inner {}}} javac Outer.java后会出现两个class文件: Outer.class…

电机计算机仿真,电机调速系统的计算机仿真

《电机调速系统的计算机仿真》由会员分享,可在线阅读,更多相关《电机调速系统的计算机仿真(11页珍藏版)》请在人人文库网上搜索。1、电机调速系统的计算机仿真一 实验目的1.通过仿真了解并会运用matlab/simulink与simpowersysdem设计电机系统并对系统进行…

Silverlight资源概述

基于 Silverlight 的应用程序可以访问以下位置中的多种资源: 宿主服务器和其他网络位置。 嵌入在应用程序包内部或外部的库程序集中的文件。 以松散形式位于应用程序包中的文件。 编译到程序集中的字体文件。 例如,资源在应用程序中可以作为错误消息…

河南农业大学计算机网络期末考试题,2017年河南农业大学信息与管理科学学院341农业知识综合三之计算机网络考研仿真模拟题...

一、选择题1. 载波侦听多路访问即CSMA ( )。A. 只用于总线拓扑结构B. 只用于环形拓扑结构C. 只用于星形拓扑结构D. 既能用于环形也能用于总线形拓扑结构【答案】A【解析】CSMA 协议是从ALOHA 协议改进而来的,而CSMA/CD协议又是在CSMA 协议的基础上改进而…

Silverlight资源文件

资源文件通常是您的应用程序使用的任意非可执行的数据文件,如图像、音频和视频文件。资源文件在某些上下文中还可能具有特定的含义。例如,在应用程序本地化的上下文中,资源文件指代 .resx 文件,您可以在本地化的附属程序集中部署这…