在Mike Taulty的视频教程中有一个动态加载silverlight应用程序的DEMO,其程序运行效果如下:
当加载dll之后:
其实实现这个的效果本身并不难,主要是这个DEMO的应用场景可以让人做很大的扩展,比如说插
件机制等.
好了,让我们看看如何实际去开发这个示例,以便让大家从流程和技术点上有个了解.
首先我们需要建立一个叫DynamicLoading的silverlight Application, 然后将如下的内容拷贝
到Page.xaml中:
<
UserControl
x:Class
="DynamicLoading.Page"
xmlns ="http://schemas.microsoft.com/client/2007"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
Width ="400" Height ="300" >
Grid x:Name ="LayoutRoot" Background ="White"
Grid.RowDeFinitions
RowDeFinition />
</ Grid.RowDeFinitions
StackPanel
TextBox Width ="200" x:Name ="txtPlugIn" Margin ="10" Text ="Implementation.dll" Button HorizontalAlignment ="Center" Width ="96" Content ="加载DLL"
Click ="OnClick"
Grid Grid.Row ="1" ="AliceBlue" ="20" ="gridHosting"
Grid StackPanel VerticalAlignment ="Bottom" TextBlock ="txtToEdit" FontSize ="16" FontStyle ="italic" Text ="编辑内容"
TextAlignment ="center" HorizontalAlignment ="Stretch" Grid.Row ="2"
VerticalAlignment ="Center"
UserControl
xmlns ="http://schemas.microsoft.com/client/2007"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
Width ="400" Height ="300" >
Grid x:Name ="LayoutRoot" Background ="White"
Grid.RowDeFinitions
RowDeFinition />
</ Grid.RowDeFinitions
StackPanel
TextBox Width ="200" x:Name ="txtPlugIn" Margin ="10" Text ="Implementation.dll" Button HorizontalAlignment ="Center" Width ="96" Content ="加载DLL"
Click ="OnClick"
Grid Grid.Row ="1" ="AliceBlue" ="20" ="gridHosting"
Grid StackPanel VerticalAlignment ="Bottom" TextBlock ="txtToEdit" FontSize ="16" FontStyle ="italic" Text ="编辑内容"
TextAlignment ="center" HorizontalAlignment ="Stretch" Grid.Row ="2"
VerticalAlignment ="Center"
UserControl
然后我们需要先建立一个接口文件(类似于插件接口),所以在当前SLN下新加一个Silverlight Class
Library 项目,名称为:Interfaces .将class.cs文件改名为:IEditText.cs .
将如下接口内容拷贝到该cs文件中:
public
class
TextChangedEventArgs : EventArgs
{
string Text { get ; set ; }
}
interface IEditText
{
UIElement GetControls();
void SetText( text);
event EventHandler < TextChangedEventArgs > TextChanged;
}
{
string Text { get ; set ; }
}
interface IEditText
{
UIElement GetControls();
void SetText( text);
event EventHandler < TextChangedEventArgs > TextChanged;
}
编译这个接口项目.接下来我们将会实现这个接口(相当于实际的插件功能).
我们再新建一个叫Implementation的Silverlight Class Library 项目,重命名class.cs文件为Editor.cs
,将如下内容拷贝到项目的cs文件中:
Editor : Interfaces.IEditText
{
TextBox textBox;
Editor()
{
textBox = new TextBox();
}
// 插件运行时加载的控件(界面) UIElement GetControls()
{
StackPanel Panel StackPanel();
StackPanel stackPanel StackPanel();
stackPanel.Margin Thickness( 5 );
stackPanel.Orientation Orientation.Horizontal;
textBox TextBox();
textBox.Width 300 ;
textBox.Height 70 ;
Button button Button();
button.Content " 点击这里 " ;
button.Click += OnButtonClick;
button.Width 60 ;
stackPanel.Children.Add(textBox);
Panel.Children.Add(stackPanel);
Panel.Children.Add(button);
return Panel;
}
text)
{
textBox.Text text;
}
将点击提交按钮时,更新相应的界面内容 OnButtonClick( object sender, EventArgs args)
{
if (TextChanged != null )
{
TextChanged( this , Interfaces.TextChangedEventArgs()
{
Text textBox.Text
});
}
}
Interfaces.TextChangedEventArgs TextChanged;
}
{
TextBox textBox;
Editor()
{
textBox = new TextBox();
}
// 插件运行时加载的控件(界面) UIElement GetControls()
{
StackPanel Panel StackPanel();
StackPanel stackPanel StackPanel();
stackPanel.Margin Thickness( 5 );
stackPanel.Orientation Orientation.Horizontal;
textBox TextBox();
textBox.Width 300 ;
textBox.Height 70 ;
Button button Button();
button.Content " 点击这里 " ;
button.Click += OnButtonClick;
button.Width 60 ;
stackPanel.Children.Add(textBox);
Panel.Children.Add(stackPanel);
Panel.Children.Add(button);
return Panel;
}
text)
{
textBox.Text text;
}
将点击提交按钮时,更新相应的界面内容 OnButtonClick( object sender, EventArgs args)
{
if (TextChanged != null )
{
TextChanged( this , Interfaces.TextChangedEventArgs()
{
Text textBox.Text
});
}
}
Interfaces.TextChangedEventArgs TextChanged;
}
之后,我们编译一下译项目.
下面看一下加载上面类库的代码(位于Page.xaml.cs文件中):
partial
Page : UserControl
{
Page()
{
InitializeComponent();
}
点击加载按钮事件 OnClick( 获取要加载的dll文件信息 assembly txtPlugIn.Text;
WebClient client WebClient();
准备获取dll文件的信息 client.OpenReadCompleted OnReadCompleted;
client.OpenReadAsync( Uri(assembly, UriKind.Relative));
}
OnReadCompleted( AssemblyPart();
获取加载的dll信息(stream格式) Assembly assembly part.Load(e.Result);
构造该对象(插件)的实例 IEditText editor
assembly.CreateInstance( Implementation.Editor ) as IEditText;
(editor )
{ 加载其中的控件(gridHosting类型为<Grid>) gridHosting.Children.Add(editor.GetControls());
editor.SetText(txtToEdit.Text);
完成事件绑定 editor.TextChanged OnTextChanged;
}
}
内容更新 OnTextChanged( e.Text;
}
}
{
Page()
{
InitializeComponent();
}
点击加载按钮事件 OnClick( 获取要加载的dll文件信息 assembly txtPlugIn.Text;
WebClient client WebClient();
准备获取dll文件的信息 client.OpenReadCompleted OnReadCompleted;
client.OpenReadAsync( Uri(assembly, UriKind.Relative));
}
OnReadCompleted( AssemblyPart();
获取加载的dll信息(stream格式) Assembly assembly part.Load(e.Result);
构造该对象(插件)的实例 IEditText editor
assembly.CreateInstance( Implementation.Editor ) as IEditText;
(editor )
{ 加载其中的控件(gridHosting类型为<Grid>) gridHosting.Children.Add(editor.GetControls());
editor.SetText(txtToEdit.Text);
完成事件绑定 editor.TextChanged OnTextChanged;
}
}
内容更新 OnTextChanged( e.Text;
}
}
当然要编译还要引用一下前面写的接口项目(Interfaces),这样代码部分就完成了.这里我们还
要将类库Implementation.dll(插件)放入web项目中的ClientBin文件夹下.这样我们就可以运行文章
开始处的页面了.
是不是很简单,这里面主要的一块内容就是使用WebClient读取dll文件,如下:
WebClient client
WebClient();
client.OpenReadCompleted OnReadCompleted;
client.OpenReadAsync( 其它的内容都是小Case:)
client.OpenReadCompleted OnReadCompleted;
client.OpenReadAsync( 其它的内容都是小Case:)
好了,今天的内容就到这里了,感兴趣的朋友可以一起来讨论:)
源码下载请 点击这里:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。