C# 我的MEF出口有什么问题吗?

C# 我的MEF出口有什么问题吗?,c#,wpf,xaml,prism,mef,C#,Wpf,Xaml,Prism,Mef,我得到一个错误: 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 t

我得到一个错误:

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设置其datacontext,MEF通过inturn导入IServiceFactory的实例我确信由于IServiceFactory和MEF而发生错误。。。。。。。。我指的是出口或进口。我猜这是因为当我删除MenuViewModel中的ImportingConstructor和IServiceFactory声明时,我的程序运行良好

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

这是我的代码:

MenuView.xaml.cs

[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class MenuView : UserControlViewBase
{
    [ImportingConstructor]
    public MenuView(MenuViewModel viewModel)
    {
        InitializeComponent();
        this.DataContext = viewModel;
    }
}
MenuViewModel.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);
            }
        }
    }

}
引导程序(WPF主应用程序)

公共类引导程序:MefBootstrapper
{
受保护的覆盖依赖对象CreateShell()
{
返回容器.GetExportedValue();
}
受保护的覆盖无效初始值设置Shell()
{
base.InitializeShell();
App.Current.MainWindow=(窗口)Shell;
App.Current.MainWindow.Show();
}
受保护的覆盖无效配置AggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(BootStrapper.Assembly));
AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(RegionNames.Assembly));
AggregateCatalog.Catalogs.Add(新的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();

    }
}
公共部分类应用程序:应用程序
{
启动时受保护的覆盖无效(StartupEventArgs e)
{
基础。启动时(e);
ObjectBase.Container=MEFLoader.Init(新列表()
{
新的AssemblyCatalog(Assembly.getExecutionGassembly())
});
BootStrapper BootStrapper=new BootStrapper();
bootstrapper.Run();
}
}
项目 这是我的项目,如果有人想看一下:


问题在于您的
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));
    }
}
公共类引导程序:MefBootstrapper
{
受保护的覆盖依赖对象CreateShell()
{
返回容器.GetExportedValue();
}
受保护的覆盖无效初始值设置Shell()
{
base.InitializeShell();
App.Current.MainWindow=(窗口)Shell;
App.Current.MainWindow.Show();
}
受保护的覆盖无效配置AggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(BootStrapper.Assembly));
AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(RegionNames.Assembly));
AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(ModuleMenu.Module.Assembly));
//新增目录
//--> 
AggregateCatalog.Catalogs.Add(新的AssemblyCatalog(typeof(ServiceFactory.Assembly));
}
}
对于引导程序配置,应用程序在启动时停止引发异常。(但它仍然没有显示任何内容)


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

您是否研究了该异常的所有内部异常?@Evk没有内部异常。可能需要在界面
IServiceFactory
中编写一些属性?例如,
[Export][PartCreationPolicy(CreationPolicy.NonShared)]
?@据我所知,接口不能用此类属性装饰谢谢先生!!!!!!你太棒了。我在客户端的MEFLoader类的MEF目录中添加了ServiceFactory实现。所以,我认为没有必要将其添加到引导程序类中。你让我开心。多谢各位。
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;
    }
}
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;

    }
}
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));
    }
}
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();

    }
}
    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));
    }
}