public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
//点击加载按钮事件
void OnClick(object sender, EventArgs args)
{
//获取要加载的dll文件信息
string assembly = txtPlugIn.Text;
WebClient client = new WebClient();
//准备获取dll文件的信息
client.OpenReadCompleted += OnReadCompleted;
client.OpenReadAsync(new Uri(assembly, UriKind.Relative));
}
void OnReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
AssemblyPart part = new AssemblyPart();
//获取加载的dll信息(stream格式)
Assembly assembly = part.Load(e.Result);
//构造该对象(插件)的实例
IEditText editor =
assembly.CreateInstance("Implementation.Editor") as IEditText;
if (editor != null)
{ //加载其中的控件(gridHosting类型为<Grid>)
gridHosting.Children.Add(editor.GetControls());
editor.SetText(txtToEdit.Text);
//完成事件绑定
editor.TextChanged += OnTextChanged;
}
}
//内容更新
void OnTextChanged(object sender, Interfaces.TextChangedEventArgs e)
{
txtToEdit.Text = e.Text;
}
}