Relationship between the two view modes

I am new to design template MVVM

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

    }

      

And the 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

    }

      

I need to know if there is a notification mecanism between the two view modes, i.e. in my case if _nbre > 5

in the first viewmodel the second viewmodel will be displayed and the value _IsBiggerthanFive

changed. So:

  • How can two view modes interact between them without instantiating in the other?
  • What is the best way to accomplish this task?
+3


source to share


2 answers


I agree with other commenters that the / pub-sub / event aggregator / messenger is a good way to go. If you are not using MVVM framework with built-in solution, I recommend this simple approach, which takes advantage of 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);
        }
    }
}

      

This is your event aggregator. Pass an instance of it to each view model and store it as a reference. Then create a class to hold the event details, say "ValueChangedEvent":

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

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

      



Post this from the first view model:

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

      

Subscribe in another class using 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();
    }
}

      

+2


source


Messenger is the solution. The MVVM Light Toolkit has an implementation of this. What you can do with it is listen to a certain type of message in your view model and process it through the messenger. http://www.mvvmlight.net/



+1


source







All Articles