实现在C#中的消息/订阅机制 [英] Implementing a message/subscription mechanism in C#

查看:140
本文介绍了实现在C#中的消息/订阅机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与原型MVVM模式的WPF应用程序。以下的答案这个问题我已经设置了 ModelProviderService 它公开的模型作为属性。该服务的消费者是的ViewModels,即他们把他们的模型,从服务,而不是实例化他们过问。

 类ModelProviderService { 

私人loginModel loginModel;
公共LoginModel LoginModel {
{返回loginModel; }
集合{loginModel =价值; }
}

私人ProjectsModel projectsModel;
公共ProjectsModel ProjectsModel {
{返回projectsModel; }
集合{projectsModel =价值; }
}

公共ModelProviderService(){
loginModel =新LoginModel();
projectsModel =新ProjectsModel();
}
}

现在,这里是要发生什么:




  • 一个ViewModel如改变的属性在 LoginModel 属性。 <; modelService.LoginModel.MyProperty = localLoginModel.MyProperty:

  • 视图模型通过设置其属性推模型属性回到服务/ LI>
  • 服务应发布一条消息:嘿,我喜欢的类型的模型 LoginModel 只是改变了

  • 已订阅此消息的任何其他视图模型将拉动这一更改的模型从服务



我如何能实现。




  1. 在广播消息?

  2. 的订阅的消息?


解决方案

您可以使用 MessageBus EventAggregator 使用弱引用发布消息给用户。看看我的实现(或的中的NuGet packgage )。



它也可以名帅消息处理到UI线程你(如果你需要更新UI组件)通过应用 HandleOnUIThreadAttribute 处理



在您的情况下使用会是这样:

  //消息
公共类LoginModelChanged
{
公共LoginModelChanged(LoginModel模型)
{
型=模型;
}

公共LoginModel型号{搞定;私人集; }
}

//服务的发布消息
公共类ModelProviderService
{
私人IMessageBus _messageBus;
私人LoginModel _loginModel;

公共ModelProviderService(IMessageBus messageBus)
{
_messageBus = messageBus;
}

公共LoginModel LoginModel
{
{返回_loginModel; }

{
_loginModel =价值;
_messageBus.Publish(新LoginModelChanged(值));
}
}
}

//订阅视图模型
公共类SomeViewModel:IHandle< LoginModelChanged>
{
公共SomeViewModel(IMessageBus messageBus)
{
messageBus.Subscribe(本);
}

公共无效手柄(LoginModelChanged消息)
{
//使用message.Model
}
} $ B $的东西b

如果你想知道更多关于内部运作以及如何实现这一点;查看源代码在 GitHub的库。随意,只要你喜欢使用代码:)


I'm prototyping a WPF application with the MVVM pattern. Following an answer to this question I have set up a ModelProviderService which exposes models as properties. The consumers of the service are the viewmodels, i.e. they pull their models from the service instead of instantiating them theirselves.

class ModelProviderService {

  private LoginModel loginModel;
  public LoginModel LoginModel {
    get { return loginModel; }
    set { loginModel = value; }
  }

  private ProjectsModel projectsModel;
  public ProjectsModel ProjectsModel {
    get { return projectsModel; }
    set { projectsModel = value; }
  }

  public ModelProviderService() {
    loginModel = new LoginModel();
    projectsModel = new ProjectsModel();
  }    
}

Now, here is what shall happen:

  • A viewmodel changes a property of e.g. the LoginModel property.
  • The viewmodel pushes the model property back to the service by setting its property: modelService.LoginModel.MyProperty = localLoginModel.MyProperty;
  • The service shall publish a message: "Hey, my model of type LoginModel just changed."
  • Any other viewmodel that has subscribed to this message will pull this changed model from the service.

How can I implement:

  1. the "broadcast message"?
  2. the subscription to the message?

解决方案

You could use a MessageBus or EventAggregator to publish messages to subscribers by using weak references. Take a look at my implementation (or the NuGet packgage).

It can also marshal the message handling to the UI thread for you (if you need to update UI components) by applying the HandleOnUIThreadAttribute on the Handle method.

Usage in your case would be something like:

// The message
public class LoginModelChanged
{
    public LoginModelChanged(LoginModel model)
    {
        Model = model;
    }

    public LoginModel Model { get; private set; }
}

// Service that publishes messages
public class ModelProviderService
{
    private IMessageBus _messageBus;
    private LoginModel _loginModel;

    public ModelProviderService(IMessageBus messageBus)
    {
        _messageBus = messageBus;
    }

    public LoginModel LoginModel
    {
        get { return _loginModel; }
        set
        {
            _loginModel = value;
            _messageBus.Publish(new LoginModelChanged(value));
        }
    }
}

// Subscribing ViewModel
public class SomeViewModel : IHandle<LoginModelChanged>
{
    public SomeViewModel(IMessageBus messageBus)
    {
        messageBus.Subscribe(this);
    }

    public void Handle(LoginModelChanged message)
    {
        // Do something with message.Model
    }
}

If you want to know more about the inner workings and how to implement this; Check out the source code in the GitHub repository. Feel free to use the code as you like :)

这篇关于实现在C#中的消息/订阅机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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