Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何让DirectoryModuleCatalog正常工作?_C#_.net_Prism_Mef_Bootstrapper - Fatal编程技术网

C# 如何让DirectoryModuleCatalog正常工作?

C# 如何让DirectoryModuleCatalog正常工作?,c#,.net,prism,mef,bootstrapper,C#,.net,Prism,Mef,Bootstrapper,我在WPF项目中使用Prism 4和MEF。我有一些DLL需要从目录中加载。这些DLL通过IGame实现IModule,并且格式正确(或者至少我认为是这样): 目前,主项目正在编译,但模块尚未初始化。我很难理解如何设置我的引导程序,文档也帮不了什么忙,因为它没有完整的DirectoryModuleCatalog示例。模块化快速启动也没有编译。这是我的引导程序: class BootStrap : MefBootstrapper { protected override

我在WPF项目中使用Prism 4和MEF。我有一些DLL需要从目录中加载。这些DLL通过IGame实现IModule,并且格式正确(或者至少我认为是这样):

目前,主项目正在编译,但模块尚未初始化。我很难理解如何设置我的引导程序,文档也帮不了什么忙,因为它没有完整的DirectoryModuleCatalog示例。模块化快速启动也没有编译。这是我的引导程序:

class BootStrap : MefBootstrapper
    {

        protected override DependencyObject CreateShell()
        {
            return ServiceLocator.Current.GetInstance<Shell>();
        }

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

        protected override void ConfigureAggregateCatalog()
        {
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrap).Assembly));  
        }


        protected override IModuleCatalog CreateModuleCatalog()
        {

            DirectoryModuleCatalog catalog = new DirectoryModuleCatalog() { ModulePath = @"..\..\..\GameTestLib\bin\Debug" };
            return catalog;
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
        }

    }
类引导:MefBootstrapper
{
受保护的覆盖依赖对象CreateShell()
{
返回ServiceLocator.Current.GetInstance();
}
受保护的覆盖无效初始值设置Shell()
{
Application.Current.MainWindow=(Window)this.Shell;
Application.Current.MainWindow.Show();
}
受保护的覆盖无效配置AggregateCatalog()
{
Add(新的AssemblyCatalog(typeof(BootStrap.Assembly));
}
受保护的覆盖IModuleCatalog CreateModuleCatalog()
{
DirectoryModuleCatalog catalog=new DirectoryModuleCatalog(){ModulePath=@.\..\..\..\GameTestLib\bin\Debug};
退货目录;
}
受保护的覆盖无效配置容器()
{
base.ConfigureContainer();
}
}

DLL的路径正确。总而言之,我的问题是:我应该如何设置引导程序?

首先,由于您使用的是Prism,我建议您使用ModuleExport,如下所示:

[ModuleExport("SnakeModule", typeof(IGame))]
但是您的问题实际上来自您没有将类设置为公共类,因此阻止了模块的发现。因此,您需要将代码更改为:

[ModuleExport("SnakeModule", typeof(IGame))]
public class SnakeModule : IGame
{
    public void Initialize()
    {
        Console.WriteLine("test");
    }

    public void StartGame()
    {
        throw new NotImplementedException();
    }

}
而且应该很好

[ModuleExport("SnakeModule", typeof(IGame))]
public class SnakeModule : IGame
{
    public void Initialize()
    {
        Console.WriteLine("test");
    }

    public void StartGame()
    {
        throw new NotImplementedException();
    }

}