加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何利用MEF中的事件聚合器?

发布时间:2020-12-15 08:16:47 所属栏目:百科 来源:网络整理
导读:我正在运行最新的PRISM 4.2.不幸的是,文档中的Event Aggregator教程是通过Unity而不是MEF驱动的.我不能让它在MEF下运行. App.xaml.cs public partial class App : Application { [Import] public IEventAggregator eventAggregator; protected override void
我正在运行最新的PRISM 4.2.不幸的是,文档中的Event Aggregator教程是通过Unity而不是MEF驱动的.我不能让它在MEF下运行.

App.xaml.cs

public partial class App : Application
    {
        [Import]
        public IEventAggregator eventAggregator;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Bootstrapper bootstrapper = new Bootstrapper();
            bootstrapper.Run();
        }
    }

Bootstrapper.cs

public class Bootstrapper : MefBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return new MainWindow();
        }

        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(this.GetType().Assembly));
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            ModuleCatalog moduleCatalog = new ModuleCatalog();

            return moduleCatalog;
        }

    }

MainWindow.xaml.cs

public partial class MainWindow : Window,IView
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }
    }

MainViewModel.cs:

[ModuleExport(typeof(MainViewModel))]
    public class MainViewModel : BindableBase,IServiceCallback
    {
        [Import]
        public IEventAggregator eventAggregator;

        [ImportingConstructor]
        public MainViewModel()
        {            
            eventAggregator.GetEvent<AppExitEvent>().Publish("");

        }
     }

尽管[import],事件聚合器在App.xaml.cs和MainViewModel中始终为null.这是为什么?
第二个问题是我是否必须将我的Viewmodels导出为模块(如上所述)以在其中使用均匀聚合器?

更新:

证明最新版本的PRISM不再支持ComposeExportedValue.

‘System.ComponentModel.Composition.Hosting.CompositionContainer’ does
not contain a definition for ‘ComposeExportedValue’ and no extension
method …

解决方法

解决这个问题的方法是SchubertJ在CodePlex的同一个问题上回答的问题:

> How to utilise Event aggregator in MEF?

作为更深入的分析,在构造函数完成之前,不会解析Properties上的Import属性.这就是为什么你需要通过构造函数注入EventAggregator依赖项作为参数,如果这个依赖项将用于构造函数实现.

因此,如果要在ViewModel构造函数上使用EventAggregator依赖项,则应使用[ImportConstructor]属性,并通过将其作为参数进行检索,向容器请求EventAggregator实例:

public class MainViewModel
{
    private readonly IEventAggregator eventAggregator;

    [ImportingConstructor]
    public MainViewModel(IEventAggregator eventAggregator)
    {
       this.eventAggregator = eventAggregator;

       this.eventAggregator.GetEvent<MyEvent>().Publish("");
    }
}

您可以在以下帖子中找到有关这两种导入替代方案的更多相关信息:

> MEF – [Import] vs [ImportingConstructor]

我希望这对你有所帮助,问候.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读