MVVM-Light工具包-如何使用PropertyChangedMessage [英] MVVM-Light Toolkit -- How To Use PropertyChangedMessage

查看:179
本文介绍了MVVM-Light工具包-如何使用PropertyChangedMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以发布正在使用的PropertyChangedMessage的工作示例吗? GalaSoft网站上的描述指出:

PropertyChangedMessage:用于广播发送者中属性已更改.与PropertyChanged事件达成相同的目的,但方式不太严格.

但是,这似乎不起作用:

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"));
    }

解决方案

丹尼尔·卡斯特罗(Daniel Castro)对我的问题进行了评论,并提出了以下问题:您希望从代码中得到什么?"

这个问题的答案促使我将这个问题写成我自己的问题.

基于对MVVM-Light文档中PropertyChangedMessage类的不良描述,我的期望是,当我发送PropertyChangedMessage时,将自动调用ViewModelBase类上的RaisePropertyChanged方法.

但是,显然是相反的情况.当您调用RaisePropertyChanged时,该方法有一个重载,您可以在其中设置一个标志,该标志确定是否将发送PropertyChangedMessage.

但是,我想要我最初期望的功能.我想发送一个新的PropertyChangedMessage,它会自动导致调用RaisePropertyChanged.这是这样做的方法.

使用以下公共NotifyPropertyChanged方法从ViewModelBase派生一个新类,该方法简单地调用受保护的RaisePropertyChanged方法:

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

然后从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);
        }
    }
}

我已经测试了这种方法,并验证了我确实可以编写如下代码来导致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;
    }
}

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"));
    }

解决方案

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;
    }
}

这篇关于MVVM-Light工具包-如何使用PropertyChangedMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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