MVVM-Light Toolkit — How To Use PropertyChangedMessage

狂风中的少年 提交于 2019-12-07 11:47:02

问题


Can someone please post a working example of the PropertyChangedMessage being used? The description from the GalaSoft site states:

PropertyChangedMessage: Used to broadcast that a property changed in the sender. Fulfills the same purpose than the PropertyChanged event, but in a less tight way.

However, this doesn't seem to work:

private bool m_value = false;
public bool Value
{
    get { return m_value ; }
    set 
    { 
        m_value = value;
        Messenger.Default.Send(new PropertyChangedMessage<bool>(m_value, true, "Value"));
    }

回答1:


This is related with the MVVM Light Messenger.

In your property definition yo use like this:

public string Name {
    get
    {
        return _name;
    }
     set
    {
        if (_name == value)
        {
            return;
        }
         var oldValue = _name;
        _name = value;
         // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
        RaisePropertyChanged(Name, oldValue, value, true);
    }
}

Then you can suscribe to any modification on the property using something like this:

Messenger.Default.Register<PropertyChangedMessage<string>>(
    this, (e) => this.Name = e.NewValue
);

Look at this post and read about the MVVM Light Messenger

To broadcast:

Messenger.Default.Send<PropertyChangedMessage<string>>(oldValue, newValue, "PropertyName");



回答2:


Daniel Castro commented on my question with the following question: "What do you expect from the code?"

The answer to this question prompted me to write this answer to my own question.

My expectations were, based on the badly written description for the PropertyChangedMessage class in the MVVM-Light documentation, that when I sent a PropertyChangedMessage then the RaisePropertyChanged method on the ViewModelBase class would get automatically called.

Apparently, however, it's the other way around. When you call RaisePropertyChanged, then that method has an overload where you can set a flag which determines whether or not a PropertyChangedMessage will be sent.

However, I want the functionality that I originally expected. I want to send off a new PropertyChangedMessage that automatically causes RaisePropertyChanged to be called. Here's how to do that.

Derive a new class from ViewModelBase with the following public NotifyPropertyChanged method which simply calls the protected RaisePropertyChanged method:

public abstract class MyViewModelBase : GalaSoft.MvvmLight.ViewModelBase
{
    public void NotifyPropertyChanged(string propertyName)
    {
        RaisePropertyChanged(propertyName);
    }
}

Then derive a new class from PropertyChangedMessage which calls the new NotifyPropertyChanged method:

public class MyPropertyChangedMessage<T> : PropertyChangedMessage<T>
{
    public MyPropertyChangedMessage(object sender, T oldValue, T newValue, string propertyName)
        : base(sender, oldValue, newValue, propertyName)
    {
        var viewModel = sender as MyViewModelBase;

        if (viewModel != null)
        {
            viewModel.NotifyPropertyChanged(propertyName);
        }
    }

    public MyPropertyChangedMessage(object sender, object target, T oldValue, T newValue, string propertyName)
        : base(sender, target, oldValue, newValue, propertyName)
    {
        var viewModel = sender as MyViewModelBase;

        if (viewModel != null)
        {
            viewModel.NotifyPropertyChanged(propertyName);
        }
    }
}

I have tested this approach and verified that I can indeed write code like the following which causes the UI to update properly:

private bool m_value = false;
public bool Value
{
    get { return m_value; }
    set
    {
        Messenger.Default.Send(new MyPropertyChangedMessage<bool>(this, m_value, value, "Value"));
        m_value = value;
    }
}


来源:https://stackoverflow.com/questions/13461278/mvvm-light-toolkit-how-to-use-propertychangedmessage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!