SilverLight学习笔记--Silverligh之动态加载程序集(.DLL)
2009-08-21 08:49
471 查看
今天我们学习如何在Silverlight中动态的加载程序集。
一、为什么我们需要动态的加载程序集:
因为在实际开发环境中,我们编写的Xap包会越来越大,此时,我们会选择把某些功能独立的部件(例如:一个计算器引擎、一段加密方法甚至一个用户界面)放置到一个程序集中,然后在需要的时候动态去加载这个程序集,并允许用户使用封装于其中的独立部件或功能,并与宿主界面进行交互。
二、如何实现
在这里,我们将以动态加载一个自定义的用户界面来学习如何实现动态地加裁程序集。
当我们点击"动态加载编辑器(Editor)"按钮后,程序就会动态地加裁我们的"编辑器程序集",它是我们编写的一个用户UI,其界面如下:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Interfaces
1.2、定义用户编辑器类
用与定义接口同样的方法,我们添加一个Silverlight类库项目,命名为:Implementation。新建后,解决方案如下图:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Interfaces; //需要引用Interfaces接口
namespace Implementation
1.3、进一步准备工作
现在我们需要生成解决方案,生成后,我们需要找到Implementation项目的Bin目录下的Debug子目录,在此子目录下找到Implementation.dll,把它拷贝到 SLDynamicLoadingAssembly.Web项目的ClientBin下备用(引用)。如下图:
<UserControl x:Class="SLDynamicLoadingAssembly.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" Background="Blue" >
<Grid x:Name="LayoutRoot" Background="Green" >
<StackPanel Margin="8,8,8,8">
<Button x:Name="btnLoadEditor" Height="42" Width="262" Content="动态加载编辑器(Editor)" Background="#FF108CF7" Click="btnLoadEditor_Click"/>
<TextBlock Text="下面的区域为外部Assembly加载区" FontSize="12" Foreground="Yellow" TextAlignment="Center"></TextBlock>
<StackPanel x:Name="hostGrid" Height="143" Width="385" RenderTransformOrigin="0.5,0.5" Background="#FFCAEE7A">
<StackPanel.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="0.002"/>
<TranslateTransform/>
</TransformGroup>
</StackPanel.RenderTransform>
</StackPanel>
<TextBlock Text="下面的区域为本地区域" Foreground="Yellow" FontSize="12" TextAlignment="Center"></TextBlock>
<StackPanel Height="60" Width="386" Background="#FF77BB33" Orientation="Horizontal">
<TextBox x:Name="txtHost" Height="33" Width="200" Text="TextBlock" TextWrapping="Wrap" Foreground="#FFCF1919" Opacity="0.99"
HorizontalAlignment="Center" FontWeight="Bold" FontFamily="Portable User Interface" TextAlignment="Center" />
<Button x:Name="btnOuterTextBox" Height="38" Width="173" HorizontalAlignment="Right" Margin="8" Content="将文本内容传入Editor" Click="btnOuterTextBox_Click" />
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
在此我们创建了用户界面。
2.2、引用前面创建的用户界面程序集:Implementation.dll
Page.xaml.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection; //因为要用到Assembly,所以引入此空间
using Interfaces;
namespace SLDynamicLoadingAssembly
至此,重新生成解决方案并运行即可看到前面的运行效果。
前往:Silverlight学习笔记清单
本文程序在Silverlight2.0和VS2008环境中调试通过。本文参照了部分网络资料,希望能够抛砖引玉,大家共同学习。
(转载本文请注明出处)
一、为什么我们需要动态的加载程序集:
因为在实际开发环境中,我们编写的Xap包会越来越大,此时,我们会选择把某些功能独立的部件(例如:一个计算器引擎、一段加密方法甚至一个用户界面)放置到一个程序集中,然后在需要的时候动态去加载这个程序集,并允许用户使用封装于其中的独立部件或功能,并与宿主界面进行交互。
二、如何实现
在这里,我们将以动态加载一个自定义的用户界面来学习如何实现动态地加裁程序集。
当我们点击"动态加载编辑器(Editor)"按钮后,程序就会动态地加裁我们的"编辑器程序集",它是我们编写的一个用户UI,其界面如下:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Interfaces
1.2、定义用户编辑器类
用与定义接口同样的方法,我们添加一个Silverlight类库项目,命名为:Implementation。新建后,解决方案如下图:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Interfaces; //需要引用Interfaces接口
namespace Implementation
1.3、进一步准备工作
现在我们需要生成解决方案,生成后,我们需要找到Implementation项目的Bin目录下的Debug子目录,在此子目录下找到Implementation.dll,把它拷贝到 SLDynamicLoadingAssembly.Web项目的ClientBin下备用(引用)。如下图:
<UserControl x:Class="SLDynamicLoadingAssembly.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" Background="Blue" >
<Grid x:Name="LayoutRoot" Background="Green" >
<StackPanel Margin="8,8,8,8">
<Button x:Name="btnLoadEditor" Height="42" Width="262" Content="动态加载编辑器(Editor)" Background="#FF108CF7" Click="btnLoadEditor_Click"/>
<TextBlock Text="下面的区域为外部Assembly加载区" FontSize="12" Foreground="Yellow" TextAlignment="Center"></TextBlock>
<StackPanel x:Name="hostGrid" Height="143" Width="385" RenderTransformOrigin="0.5,0.5" Background="#FFCAEE7A">
<StackPanel.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="0.002"/>
<TranslateTransform/>
</TransformGroup>
</StackPanel.RenderTransform>
</StackPanel>
<TextBlock Text="下面的区域为本地区域" Foreground="Yellow" FontSize="12" TextAlignment="Center"></TextBlock>
<StackPanel Height="60" Width="386" Background="#FF77BB33" Orientation="Horizontal">
<TextBox x:Name="txtHost" Height="33" Width="200" Text="TextBlock" TextWrapping="Wrap" Foreground="#FFCF1919" Opacity="0.99"
HorizontalAlignment="Center" FontWeight="Bold" FontFamily="Portable User Interface" TextAlignment="Center" />
<Button x:Name="btnOuterTextBox" Height="38" Width="173" HorizontalAlignment="Right" Margin="8" Content="将文本内容传入Editor" Click="btnOuterTextBox_Click" />
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
在此我们创建了用户界面。
2.2、引用前面创建的用户界面程序集:Implementation.dll
Page.xaml.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection; //因为要用到Assembly,所以引入此空间
using Interfaces;
namespace SLDynamicLoadingAssembly
至此,重新生成解决方案并运行即可看到前面的运行效果。
前往:Silverlight学习笔记清单
本文程序在Silverlight2.0和VS2008环境中调试通过。本文参照了部分网络资料,希望能够抛砖引玉,大家共同学习。
(转载本文请注明出处)
相关文章推荐
- 忆龙2009:Silverlight学习笔记-根据需要动态加载应用程序集
- Silverlight学习笔记十二动态加载图片和显示提示(ToolTip)
- Silverlight学习笔记(二)-----XamlReader 动态加载SilverLight 对象
- 忆龙2009:Silverlight学习笔记-根据需要动态加载应用程序集
- 孙鑫关于DLL(Dynamic Link Library)动态链接库的视频讲座笔记和学习小结
- 学习笔记:ListView的动态加载。
- C#动态加载DLL,通过配置文件实现对程序集的即插即用
- as3学习笔记4:URLLoader动态加载XML
- 也说说Silverlight动态加载DLL(转)
- CUBRID学习笔记 14 dll加载错误
- C++Builder学习笔记5.1(连接数据库,读写配置文件,加载DLL)
- ext4 学习笔记(八)[动态加载Js](白鹤翔第一季)
- SilverLight学习笔记--Silverligh之在Javascript中调用.NET(包括简单参数和复杂参数的操作)
- iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
- perl学习笔记(4)——动态加载
- iPhone开发学习笔记002――Xib设计UITableViewCell然后动态加载
- 【JavaEE】JavaEE学习笔记之基础加强(类加载器,注解,动态代理)
- HTML5学习笔记(二十三):DOM应用之动态加载脚本
- [WPF学习笔记]动态加载XAML
- 【JAVAWEB学习笔记】25_基础加强:类加载器、注解 @xxx和动态代理