我建議你使用MEF來導入和導出不同DLL的信息。每個DLL可以有資源(如XAML文件)以供查看。所以,我的選擇是MVVM + MEF模塊化+ Prism EventAggregator用於交互模塊。 一些樣品,只是一個概念演示:
'單經理' 的種類:
public class AddinsManager : IAddinsManager
{
[ImportMany(typeof(IModule))]
private OrderingCollection<IModule, IOrderMetadata> modules = new OrderingCollection<IModule, IOrderMetadata>(lazyRule => lazyRule.Metadata.Order);
private CompositionContainer container;
private bool isInitialized = false;
/// <summary>
/// Gets available application modules (add-ins)
/// </summary>
public List<IModule> Modules { get; private set; }
/// <summary>
/// Initialize manager
/// </summary>
public void Initialize()
{
Assembly oAssembly = Assembly.GetAssembly(Application.Current.GetType());
this.container = new CompositionContainer(GetCatalog(oAssembly));
this.container.ComposeParts(this, Context.Current);
this.Modules = this.modules
.Select(lazy => lazy.Value)
.ToList();
this.isInitialized = true;
}
/// <summary>
/// Initialize modules
/// </summary>
public void InitializeModules()
{
foreach (IModule oModule in this.Modules)
{
oModule.Initialize();
}
}
/// <summary>
/// Injects dependencies in specified container
/// </summary>
/// <param name="host">Container to inject in</param>
public void Compose(object host)
{
if (host == null)
{
throw new ArgumentNullException();
}
this.EnsureInitialize();
this.container.ComposeParts(host);
}
/// <summary>
/// Register views of the modules
/// </summary>
public void RegisterViews()
{
this.EnsureInitialize();
foreach (IModule oModule in this.Modules)
{
foreach (Uri oUri in oModule.GetViewPath().ToArray())
{
ResourceDictionary oDictionary = new ResourceDictionary();
oDictionary.Source = oUri;
Application.Current.Resources.MergedDictionaries.Add(oDictionary);
}
}
}
/// <summary>
/// Get catalog for modules load
/// </summary>
/// <param name="assembly">Assembly to search modules</param>
/// <returns>Catalog for modules load</returns>
private static AggregateCatalog GetCatalog(Assembly assembly)
{
string sDirName = Path.GetDirectoryName(assembly.Location);
DirectoryCatalog oDirCatalog = new DirectoryCatalog(sDirName, "Company.*");
AssemblyCatalog oAssemblyCatalog = new AssemblyCatalog(assembly);
AggregateCatalog oCatalog = new AggregateCatalog(oAssemblyCatalog, oDirCatalog);
return oCatalog;
}
/// <summary>
/// Ensure if manager was initialized
/// </summary>
private void EnsureInitialize()
{
if (!this.isInitialized)
{
throw new Exception("Add-ins Manager Component not initialized");
}
}
}
模塊(DLL單獨的項目)實施
[Export(typeof(IModule))]
public class LayoutsModule : AModule
{
private static LayoutsVM viewModel;
/// <summary>
/// Gets reporting add-in main view model
/// </summary>
public static LayoutsVM ViewModel
{
get
{
if (viewModel == null)
{
viewModel = Context
.Current
.LayoutManager
.ModulesVM
.OfType<LayoutsVM>()
.FirstOrDefault();
}
return viewModel;
}
}
/// <summary>
/// Initialize module
/// </summary>
public override void Initialize()
{
Context
.Current
.EventAggregator
.GetEvent<MenuInitializing>()
.Subscribe(this.InitializeMenu);
base.Initialize();
}
}
查看嵌入式資源(View.xaml )在每個DLL上。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Company.Client.ViewModels">
<DataTemplate DataType="{x:Type vm:ApplicationVM}">
<ContentPresenter Content="{Binding AddinsViewModel}" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:MenuVM}">
<ContentPresenter Content="{Binding RibbonData}" />
</DataTemplate>
</ResourceDictionary>
我使用MEF在我的模塊導出和導入登錄信息 – blindmeis