私はで述べたようなことをしましたDataTemplates。MEFを使用してプラグインをロードし、起動時にとDictionaryへの参照を含むをロードしました。プラグインは、3つの主要コンポーネントを使用して構築されています。ViewModelView
IBasePlugin.cs
このシンプルなインターフェースにより、プラグインのスケルトンを作成できます。これは、を使用しImportてメインアプリケーションへのプラグインに使用するものであるため、非常に基本的なもののみが含まれますMEF。
public interface IBasePlugin
{
WorkspaceViewModel ViewModel { get; }
ResourceDictionary View{ get; }
}
Plugin.cs
次の部分はPlugin.csファイルです。プラグインのすべてのプロパティと、必要なすべての参照が含まれています。View&などViewModel。
[Export(typeof(IBasePlugin))]
public class Plugin : IBasePlugin
{
[Import]
private MyPluginViewModel _viewModel { get; set; }
private ResourceDictionary _viewDictionary = new ResourceDictionary();
[ImportingConstructor]
public Plugin()
{
// First we need to set up the View components.
_viewDictionary.Source =
new Uri("/Extension.MyPlugin;component/View.xaml",
UriKind.RelativeOrAbsolute);
}
....Properties...
}
View.xaml
これにはDataTemplate、プラグインViewとへの参照が含まれていますViewModel。これはPlugin.cs、メインアプリケーションにロードするために使用するものです。これにより、アプリケーションは、WPFすべてをバインドする方法を知ることができます。
<DataTemplate DataType="{x:Type vm:MyPluginViewModel}">
<vw:MyPluginView/>
次に、MEFを使用してすべてのプラグインをロードし、プラグインの処理を担当するワークスペースにフィードして、使用可能なすべてのプラグインを表示するために使用されるにViewModel保存します。ObservableCollection
プラグインのロードに使用するコードは、次のようになります。
var plugins = Plugins.OrderBy(p => p.Value.ViewModel.HeaderText);
foreach (var app in plugins)
{
// Take the View from the Plugin and Merge it with,
// our Applications Resource Dictionary.
Application.Current.Resources.MergedDictionaries.Add(app.Value.View)
// THen add the ViewModel of our plugin to our collection of ViewModels.
var vm = app.Value.ViewModel;
Workspaces.Add(vm);
}
との両方がプラグインからアプリケーションにロードされるDictinoaryとViewModel、たとえばを使用してコレクションを表示できますTabControl。
<TabControl ItemsSource="{Binding Workspaces}"/>
私もここで同様の答えを出しましたが、あなたが面白いと思うかもしれないいくつかの追加の詳細があります。