两个视图模型之间的通信 [英] Communication between two viewmodels

查看:38
本文介绍了两个视图模型之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 MVVM 设计模式的新手,并且拥有以下viewmodels:

I'm newbie in MVVM design pattern, and I have these viewmodels :

ClassAViewModel

public class ClassAViewModel : INotifyPropertyChanged
    {
        private int _nbre = 0;

        public int Nbre
        {
            get
            {
                return _nbre;
            }
            set
            {
                _nbre = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Nbre"));
            }
        }

        #region Events
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

    }

ClassBViewModel

 PUBLIC class ClassBViewModel: INotifyPropertyChanged
    {
        private Boolean _IsBiggerthanFive = false;

        public bool IsBiggerthanFive
        {
            get
            {
                return _IsBiggerthanFive;
            }
            set
            {
                _IsBiggerthanFive = value;
                PropertyChanged(this, new PropertyChangedEventArgs("IsBiggerthanFive"));
            }
        }

        #region Events
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

    }

我需要知道两个视图模型之间是否存在通知机制,即在我的情况下 _nbre>5 在第一个视图模型中,将通知第二个视图模型,并更改 _IsBiggerthanFive 的值.所以:

I need to know if a mecanism of notification between two viewmodels exists , ie in my case if _nbre > 5 in the first viewmodel, the second viewmodel will be notified and the value of _IsBiggerthanFive will be changed. So:

  1. 两个视图模型如何在它们之间相互通信而又不彼此实例化?
  2. 完成这项任务的最佳方法是什么?

推荐答案

我同意其他评论者的观点,认为mediator/pub-sub/event聚合器/messenger是一个不错的选择.如果您不将MVVM框架与内置解决方案一起使用,那么我建议利用响应式扩展的简单方法:

I agree with other commenters that the mediator/pub-sub/event aggregator/messenger is a good way to go. If you're not using an MVVM framework with a built-in solution, then I recommend this simple approach that takes advantage of the Reactive extensions:

public class EventPublisher : IEventPublisher
{
    private readonly ConcurrentDictionary<Type, object> subjects
        = new ConcurrentDictionary<Type, object>();

    public IObservable<TEvent> GetEvent<TEvent>()
    {
        var subject =
            (ISubject<TEvent>) subjects.GetOrAdd(typeof (TEvent),
                        t => new Subject<TEvent>());
        return subject.AsObservable();
    }

    public void Publish<TEvent>(TEvent sampleEvent)
    {
        object subject;
        if (subjects.TryGetValue(typeof(TEvent), out subject))
        {
            ((ISubject<TEvent>)subject)
                .OnNext(sampleEvent);
        }
    }
}

这就是您的整个事件汇总器.将其实例传递到每个视图模型中,并将其存储为参考.然后创建一个类来存储您的事件详细信息,比如说"ValueChangedEvent":

That's your whole event aggregator. Pass an instance of it into each view model, and store it as a reference. Then create a class to store your event details, let's say "ValueChangedEvent":

public class ValueChangedEvent
{
    public int Value
    {
        get { return _value; }
    }
    private readonly int _value;

    public ValueChangedEvent(int value)
    {
        _value = value;
    }
}

从第一个视图模型中像这样发布:

Publish like this from the first view model:

set
{
    _nbre = value;
    PropertyChanged(this, new PropertyChangedEventArgs("Nbre"));
    _eventPublisher.Publish(new ValueChangedEvent(value));
}

使用 GetEvent 订阅另一个类:

public class ClassBViewModel: INotifyPropertyChanged, IDisposable
{
    private readonly IDisposable _subscriber;

    public ClassBViewModel(IEventPublisher eventPublisher)
    {
        _subscriber = eventPublisher.Subscribe<ValueChangedEvent>(next => 
        {
            IsBiggerthanFive = next.Value > 5;
        });
    }

    public void Dispose()
    {
        _subscriber.Dispose();
    }
}

这篇关于两个视图模型之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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