忆龙2009:Silverlight学习笔记-根据需要动态加载应用程序集

      下面介绍了一种方法,在当前程序集中动态向主机服务器检索并加载程序集的方法。该方法响应用户鼠标单击事件,使用 WebClient 类启动程序集的异步下载,在程序集下载完成后,使用 AssemblyPart 类来动态加载此程序集。

      要实现这个功能,您的应用程序项目中必须已经引用了该程序集,同时,该程序集的"复制本地"值还必须为"False",这样,它将不会部署在应用程序包中。

view plaincopy to clipboardprint?
<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> 
<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>

view plaincopy to clipboardprint?
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);  
        }  
    }  

你可能感兴趣的:(silverlight)