您的位置:首页 > 其它

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环境中调试通过。本文参照了部分网络资料,希望能够抛砖引玉,大家共同学习。

(转载本文请注明出处)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: