如何使用Prism 6在不同的MVVM之间发送消息或事件 [英] How to send message or event between different MVVM using Prism 6

查看:572
本文介绍了如何使用Prism 6在不同的MVVM之间发送消息或事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


我正在为客户项目构建一个应用程序,并且要求使用Prism。

在我的应用程序中不同的View和ViewModels作为普通的WPF应用架构。


我需要设置的是一项负责向订户发送事件的服务。


  1. 我有一个PRISM shell应用程序,它是带有不同工具栏按钮的主窗口。那些按钮点击事件需要通过对此类事件感兴趣的适当视图来接收。
  2. 我还有具有自己命令的视图,并且可以将事件发送到其他视图


基于这2个场景,我有2个问题:


  1. 我该如何定义
  2. 我应该设置1个全局事件服务还是将其拆分为不同的事件服务(ShellCommand和ViewCommand服务)


<事件服务,找不到步骤bs步骤示例

  • p>


    问候


    解决方案

    In Prism这有点令人讨厌。


    Eventaggregator是要走的路:


    https://msdn.microsoft.com/en-us/library/ff921122.aspx?f=255&MSPPError=-2147217396


    当您查看代码时,会注意到重复的引用,例如:

     eventAggregat or.GetEvent 


    eventAggregator是eventaggregator对象的一个​​实例。

    您需要传递相同的实例,以便发布者可以在其上举办活动,订阅者可以订阅该活动。是的。这很笨重。 


    如果您还使用Unity,那么您可以解析eventAggregator 输出您的ioc容器并使其成为单个对象,这样您每次都可以获得相同的实例。 (那个 容器也是一个实例对象。)


    如果没有Unity,那么你可以在第一次新建它时将你的对象缓存在Application.current.resources中,然后再抓回来每当你想使用它时都在那里。



    你可以向Application添加一个属性来保存对象,以这种方式共享它。 / p>


    Dear all,

    I am building an application for a customer project and the requirement is to use Prism.
    In the application I have different View and ViewModels as a normal WPF app architecture.

    What I need to set up is a service which is in charge to send events to subscribers.

    1. I have a PRISM shell app which is the main window with different tool bar button. Those button click event need to be receive by proper view interested on such event.
    2. I have also Views which have theire own commands and can send events to other views as well

    Base on this 2 scenarios, I have 2 questions :

    1. How can I define the event service, could not find step bs step exemple
    2. Should I setup 1 global event service or split it different event services (ShellCommand and ViewCommand service)

    Regards

    解决方案

    In Prism this is a bit of a nuisance.

    Eventaggregator is the way to go:

    https://msdn.microsoft.com/en-us/library/ff921122.aspx?f=255&MSPPError=-2147217396

    When you look at the code there notice repeated references like:

     eventAggregator.GetEvent

    That eventAggregator is an instance of the eventaggregator object.

    You need to pass the same instance of that around so the publisher can raise an event on it and the subscribers can subscribe to that event. Yep. It's clunky. 

    If you're also using Unity then you can resolve eventAggregator out your ioc container and make it a singleton object so you get the same instance each time. ( That container is an instance object as well ).

    If no Unity then you could cache your object in Application.current.resources when you first new it up and then grab it back out there whenever you want to use it.

    Or

    You could add a property to Application to hold the object, share it that way.


    这篇关于如何使用Prism 6在不同的MVVM之间发送消息或事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆