忆龙2009:Silverlight学习笔记-根据需要动态加载应用程序集
2010-01-05 23:53
537 查看
下面介绍了一种方法,在当前程序集中动态向主机服务器检索并加载程序集的方法。该方法响应用户鼠标单击事件,使用 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);
}
}
}
<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);
}
}
}
相关文章推荐
- 忆龙2009:Silverlight学习笔记-根据需要动态加载应用程序集
- SilverLight学习笔记--Silverligh之动态加载程序集(.DLL)
- Silverlight学习笔记十二动态加载图片和显示提示(ToolTip)
- Silverlight学习笔记(二)-----XamlReader 动态加载SilverLight 对象
- HTML5学习笔记(二十三):DOM应用之动态加载脚本
- 【JAVAWEB学习笔记】25_基础加强:类加载器、注解 @xxx和动态代理
- Unity学习笔记13——代码动态加载Prefab预设体
- System.Lazy<T>延迟加载 在很多情况下,有些对象需要在使用时加载或根据逻辑动态加载。有些情况如果不延迟加载,可能会影响效率甚至抛出Timeout Exception。如网络操作、数据库操
- Java学习笔记-动态加载类
- android 开发零起步学习笔记(十):UI控件动态加载机制浅析
- iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
- nGrinder学习笔记 — 根据需要输出log等级
- Android JNI学习笔记——so文件动态加载
- 【AngularJS学习笔记】用打包代替动态加载
- 学习笔记:ListView的动态加载。
- Cedar老师的java中的反射学习笔记(二)--动态加载
- iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
- 反射 学习笔记之动态加载类
- wince驱动学习笔记(vs2005实现流驱动动态加载与卸载 1)
- Silverlight学习笔记三(鼠标点击动态画直线|动态设置Ellipse的Canvas.Top与Canvas.Left|动态设置Stroke属性的方法。)