我正在研究Silverlight 4 RC中的一個應用程序,我正在考慮利用oppertunity來學習處理插件控件的MEF。我已經在一個非常基本的莊園裏工作,但它並不完整,我知道有更好的方法來導入多個xap。MEF + SL4問題
本質上說,在我的主機應用程序的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插件的,我堆棧面板時填充如下:
TestPlugIn一個 TestPlugIn一個 TestPlugIn乙
我顯然在這裏錯過了一些東西。任何人都可以指出我應該做什麼? 謝謝!
謝謝,這幾乎是我已經走了,似乎可以滿足我的需求 – 2010-03-18 17:23:59