gpt4 book ai didi

c# - 我的 MEF 导出有任何问题吗?

转载 作者:行者123 更新时间:2023-11-30 23:25:48 28 4
gpt4 key购买 nike

我得到一个错误:

An exception has occurred while trying to add a view to region 'MenubarRegion'. 
- The most likely causing exception was was:
'Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured
while trying to get instance of type MenuView, key "" --->

我的 MenuView 使用 MEF 通过 MenuViewModel 设置其数据上下文,这又导入了一个 IServiceFactory 实例。 我确定错误是由于 IServiceFactory 和 MEF 而发生的。......我的意思是导出或导入。我猜这是因为当我删除 MenuViewModel 中的 ImportingConstructor 和 IServiceFactory 声明时,我的程序运行良好。

我已经使用 MefX 检查了 MEF 上的错误。以下是结果:

enter image description here

enter image description here

enter image description here

这是我的代码:

菜单 View .xaml.cs

[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class MenuView : UserControlViewBase
{
[ImportingConstructor]
public MenuView(MenuViewModel viewModel)
{
InitializeComponent();
this.DataContext = viewModel;
}
}

菜单 View 模型.cs

[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MenuViewModel : ViewModelBase
{

IServiceFactory _ServiceFactory;

[ImportingConstructor]
public MenuViewModel(IServiceFactory serviceFactory)
{
_ServiceFactory = serviceFactory;
}

protected override void OnViewLoaded()
{
_MenuItems = new ObservableCollection<MenuItem>();

WithClient<IMenuItemService>(_ServiceFactory.CreateClient<IMenuItemService>(), menuItemClient =>
{
MenuItem[] menuItems = menuItemClient.GetAllParentMenuItemsWithChildren();
if (menuItems != null)
{
foreach (MenuItem menuItem in menuItems)
{
_MenuItems.Add(menuItem);
}

_SelectedMenuItem = _MenuItems[2];
}

});
}

private ObservableCollection<MenuItem> _MenuItems;

public ObservableCollection<MenuItem> MenuItems
{
get
{
return _MenuItems;
}
set
{
if (_MenuItems != value)
{
_MenuItems = value;
OnPropertyChanged(() => MenuItems, false);
}
}
}

private MenuItem _SelectedMenuItem;

public MenuItem SelectedMenuItem
{
get
{
return _SelectedMenuItem;
}
set
{
if (_SelectedMenuItem != value)
{
_SelectedMenuItem = value;
OnPropertyChanged(() => SelectedMenuItem);
}
}
}

}

IServiceFactory.cs

public interface IServiceFactory
{
T CreateClient<T>() where T : IServiceContract;
}

服务工厂.cs

[Export(typeof(IServiceFactory))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ServiceFactory : IServiceFactory
{
public T CreateClient<T>() where T : IServiceContract
{
return ObjectBase.Container.GetExportedValue<T>();
}
}

Bootstrap (客户端):

public static class MEFLoader
{
public static CompositionContainer Init()
{
return Init(null);
}

public static CompositionContainer Init(ICollection<ComposablePartCatalog> catalogParts)
{
AggregateCatalog catalog = new AggregateCatalog();

catalog.Catalogs.Add(new AssemblyCatalog(typeof(MenuItemClient).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MEFLoader).Assembly));

if (catalogParts != null)
foreach (var part in catalogParts)
catalog.Catalogs.Add(part);

CompositionContainer container = new CompositionContainer(catalog);

return container;
}
}

Bootstrap (业务端)

public static class MEFLoader
{
public static CompositionContainer Init()
{
AggregateCatalog catalog = new AggregateCatalog();

catalog.Catalogs.Add(new AssemblyCatalog(typeof(MunimPlusEngine).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MenuItemManager).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MenuItemRepository).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MEFLoader).Assembly));

CompositionContainer container = new CompositionContainer(catalog);

return container;

}
}

Bootstrap (WPF 主应用程序)

public class BootStrapper : MefBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<Shell>();
}

protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
}

protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleMenu.Module).Assembly));
}
}

App.xaml.cs

public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

ObjectBase.Container = MEFLoader.Init(new List<ComposablePartCatalog>()
{
new AssemblyCatalog(Assembly.GetExecutingAssembly())
});

BootStrapper bootstrapper = new BootStrapper();
bootstrapper.Run();

}
}

项目

如果有人想看一下,这是我的项目:

Download Project

最佳答案

问题是您的 ServiceFactory 实现没有添加到 MEF 目录中。添加时:

    public class BootStrapper : MefBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<Shell>();
}

protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
}

protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleMenu.Module).Assembly));
//Added catalog
//-->
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ServiceFactory).Assembly));
}
}

对于您的 Bootstrap 配置,应用程序在启动时停止抛出异常。 (但是它仍然没有显示任何内容)

为了添加该类型,我需要在主应用程序中添加对其他项目的引用。

关于c# - 我的 MEF 导出有任何问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102027/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com