【发布时间】:2010-03-17 15:38:10
【问题描述】:
我正在开发 Silverlight 4 RC 中的应用程序,我正在借此机会学习 MEF 以处理插件控件。我已经让它在一个非常基本的庄园中工作,但它并不完全整洁,而且我知道有更好的方法来导入多个 xap。
基本上,在我的宿主应用程序的 App.xaml 中,我有以下告诉 MEF 加载我的 xap:
AggregateCatalog catalog = new AggregateCatalog();
DeploymentCatalog c1 = new DeploymentCatalog(new Uri("TestPlugInA.xap", UriKind.Relative));
DeploymentCatalog c2 = new DeploymentCatalog(new Uri("TestPlugInB.xap", UriKind.Relative));
catalog.Catalogs.Add(c1);
catalog.Catalogs.Add(c2);
CompositionHost.Initialize(catalog);
c1.DownloadAsync();
c2.DownloadAsync();
我确定我在这里没有完全使用 AggregateCatalog,我需要能够加载目录中可能存在的任何 xap,而不仅仅是硬编码 Uri……
此外,在主机的 MainPage.xaml.cs 中,我有以下 MEF 将插件放入的集合:
[ImportMany(AllowRecomposition = true)]
public ObservableCollection<IPlugInApp> PlugIns { get; set; }
再次,这有效,但我很确定我使用 ImportMany 不正确......
最后,MainPage.xaml.cs 文件实现了 IPartImportsSatisfiedNotification,我有以下处理插件的加载:
public void OnImportsSatisfied()
{
sp.Children.Clear();
foreach (IPlugInApp plugIn in PlugIns)
{
if (plugIn != null)
sp.Children.Add(plugIn.GetUserControl());
}
}
这可行,但运行 n 次(n 是要加载的 xap 的数量)似乎很脏。我不得不调用 sp.Children.Clear() 就好像我没有一样,在加载 2 个插件时,我的堆栈面板填充如下:
测试插件 A 测试插件 A 测试插件B
我显然在这里遗漏了一些东西。谁能指出我应该做什么? 谢谢!
【问题讨论】:
标签: silverlight-4.0 mef