wpf MVVM-Light 工具包——如何使用 PropertyChangedMessage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13461278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVVM-Light Toolkit -- How To Use PropertyChangedMessage
提问by Big Tuna
Can someone please post a working example of the PropertyChangedMessage being used? The description from the GalaSoft site states:
有人可以发布一个正在使用的 PropertyChangedMessage 的工作示例吗?GalaSoft 网站的描述指出:
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.
PropertyChangedMessage:用于广播发送方的属性已更改。实现与 PropertyChanged 事件相同的目的,但不那么严格。
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"));
}
回答by Salvador Sarpi
This is related with the MVVM Light Messenger.
这与 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 postand read about the MVVM Light Messenger
查看这篇文章并阅读有关MVVM Light Messenger 的信息
To broadcast:
广播:
Messenger.Default.Send<PropertyChangedMessage<string>>(oldValue, newValue, "PropertyName");
回答by Big Tuna
Daniel Castro commented on my question with the following question: "What do you expect from the code?"
丹尼尔·卡斯特罗 (Daniel Castro) 用以下问题评论了我的问题:“您对代码有何期望?”
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.
我的期望是,基于 MVVM-Light 文档中对 PropertyChangedMessage 类的糟糕描述,当我发送 PropertyChangedMessage 时,ViewModelBase 类上的 RaisePropertyChanged 方法将被自动调用。
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.
然而,显然,情况正好相反。当您调用 RaisePropertyChanged 时,该方法有一个重载,您可以在其中设置一个标志来确定是否发送 PropertyChangedMessage。
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.
但是,我想要我最初期望的功能。我想发送一个新的 PropertyChangedMessage,它会自动导致 RaisePropertyChanged 被调用。这是如何做到这一点。
Derive a new class from ViewModelBase with the following public NotifyPropertyChanged method which simply calls the protected RaisePropertyChanged method:
使用以下公共 NotifyPropertyChanged 方法从 ViewModelBase 派生一个新类,该方法仅调用受保护的 RaisePropertyChanged 方法:
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:
然后从 PropertyChangedMessage 派生一个新类,它调用新的 NotifyPropertyChanged 方法:
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:
我已经测试了这种方法并验证我确实可以编写如下代码,这会导致 UI 正确更新:
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;
}
}

