Prism v4、MEF 服务

发布于 2024-10-16 09:57:55 字数 1742 浏览 3 评论 0原文

我有一个 WPF Windows 应用程序,它使用 ms 功能区控件作为菜单。在我的基础设施项目中,我希望有一个将在所有模块中引用的共享服务。然后,每个模块将使用该服务来定义应为该模块显示哪些菜单项。

我读了这个 Prism+MEF:延迟了从 prism-module 的服务导出< /a> 但无法让我的其他模块识别该服务。

服务

namespace Infrastructure
{
    [ModuleExport("InfModule", typeof(InfModule), InitializationMode = InitializationMode.WhenAvailable)]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class InfModule : IModule
    {
        [Export(typeof(IMenuService))]
        public IMenuService MenuService { get; private set; }

        public void Initialize()
        {
            MenuService = new MenuService();

            MenuService.AddItem("test");
        }
    }
}

模块

namespace Classic
{
    [ModuleExport("Classic", typeof(Classic), InitializationMode = InitializationMode.WhenAvailable)]
    [ModuleDependency("InfModule")]
    public class Classic : IModule
    {
        private IRegionManager _regionManager;

        [Import(typeof(IMenuService))]
        private IMenuService menuService { get; set; }

        [ImportingConstructor]
        public Classic(IRegionManager regionManager)
        {
            this._regionManager = regionManager;

            // This shows as true
            Debug.WriteLine(menuService == null);
        }

        public void Initialize()
        {
            _regionManager.RegisterViewWithRegion("RibbonRegion", typeof(Views.RibbonTabMenu));

            // This shows as true
            Debug.WriteLine(menuService == null);
        }
    }
}

我本以为调试行之一会在导入后输出为 false。知道我缺少什么吗?

I have a WPF windows application that uses the ms ribbon control for the menu. In my infrastructure project I want to have a shared service that will be referenced in all modules. Each module will then use that service to define what menu items should be displayed for the module.

I read this Prism+MEF: delayed a service export from prism-module but can't get my other modules to recognize the service.

The service

namespace Infrastructure
{
    [ModuleExport("InfModule", typeof(InfModule), InitializationMode = InitializationMode.WhenAvailable)]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class InfModule : IModule
    {
        [Export(typeof(IMenuService))]
        public IMenuService MenuService { get; private set; }

        public void Initialize()
        {
            MenuService = new MenuService();

            MenuService.AddItem("test");
        }
    }
}

The module

namespace Classic
{
    [ModuleExport("Classic", typeof(Classic), InitializationMode = InitializationMode.WhenAvailable)]
    [ModuleDependency("InfModule")]
    public class Classic : IModule
    {
        private IRegionManager _regionManager;

        [Import(typeof(IMenuService))]
        private IMenuService menuService { get; set; }

        [ImportingConstructor]
        public Classic(IRegionManager regionManager)
        {
            this._regionManager = regionManager;

            // This shows as true
            Debug.WriteLine(menuService == null);
        }

        public void Initialize()
        {
            _regionManager.RegisterViewWithRegion("RibbonRegion", typeof(Views.RibbonTabMenu));

            // This shows as true
            Debug.WriteLine(menuService == null);
        }
    }
}

I would have expected one of the debug lines to output as false since its imported. Any idea's what I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-10-23 09:57:55

运行构造函数时永远不会设置属性导入,因为在构造对象之前无法设置对象的属性。

另一个问题是在 InfModule 中,您设置导出的值太晚了。 MEF 仅查看一次导出的值,之后它会缓存该值并且不会再次调用 getter。在本例中,它是在调用 Initialize() 之前获取导出。设置导出的逻辑需要从构造函数或属性 getter 中的代码运行。

Property imports will never be set while running the constructor, since you can't set properties on an object until it's constructed.

The other problem is that in InfModule, you are setting the exported value too late. MEF only looks at the value for an export once, after that it caches the value and doesn't call the getter again. In this case it is getting the export before Initialize() is called. The logic to set the export needs to either run from the constructor or from code in the property getter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文