MVMLight 消息传递和 Silverlight

发布于 2024-09-15 08:41:00 字数 3541 浏览 1 评论 0原文

我正在尝试使用 MVVM Light 和消息传递类来获取一个示例。在示例中,我有一个从 Silveright 4 的 MVVM 模板创建的测试项目。我在主页上添加了一个按钮。单击该按钮时,它会更新 ViewModel 上的属性。当属性更新时,我想显示一个带有新值的消息框。

关键代码行是:

Messenger.Default.Register(this, new Action(ShowMessage));

我可以让它在 WPF 中工作,但不能在 silverlight 中工作。当属性更改时,它应该使用字符串参数调用 ShowMessage,但事实并非如此。如果我使用:

Messenger.Default.Send("Hello MVVM");

此操作有效,字符串将作为消息发送到 ShowMessage。

但是,如果属性更改,则不会发送该消息,即使该属性是使用 MVVMINPC 代码段创建的并且具有以下行:

RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);

这应该与 Messager.Default.Send 具有相同的效果,但它似乎被忽略。 ThePropertyChangedEvent 确实被引发,但消息部分似乎已断开连接。

我做错了什么吗?这是完整的 MainViewModel:

public class MainViewModel : ViewModelBase
{
    public RelayCommand MyRelayCommand { get; set; }
    public const string MyPropertyPropertyName = "MyProperty";
    private string _myProperty = "test";

    public string MyProperty
    {
        get
        {
            return _myProperty;
        }

        set
        {
            if (_myProperty == value)
            {
                return;
            }

            var oldValue = _myProperty;
            _myProperty = value;
            RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);
        }
    }

    public void DoSomething()
    {
        //Messenger.Default.Send("Hello MVVM"); //Works
        this.MyProperty = "Hello World"; //Doesn't work.
    }



    public void ShowMessage(string message)
    {
        MessageBox.Show(message);
    }

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {

        Messenger.Default.Register(this, new Action<string>(ShowMessage));

        MyRelayCommand = new RelayCommand(new Action(DoSomething));

        this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MainViewModel_PropertyChanged);

    }

    void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        MessageBox.Show(e.PropertyName);
    }


}public class MainViewModel : ViewModelBase
{
    public RelayCommand MyRelayCommand { get; set; }
    public const string MyPropertyPropertyName = "MyProperty";
    private string _myProperty = "test";

    public string MyProperty
    {
        get
        {
            return _myProperty;
        }

        set
        {
            if (_myProperty == value)
            {
                return;
            }

            var oldValue = _myProperty;
            _myProperty = value;
            RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);
        }
    }

    public void DoSomething()
    {
        //Messenger.Default.Send("Hello MVVM"); //Works
        this.MyProperty = "Hello World"; //Doesn't work.
    }



    public void ShowMessage(string message)
    {
        MessageBox.Show(message);
    }

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {

        Messenger.Default.Register(this, new Action<string>(ShowMessage));

        MyRelayCommand = new RelayCommand(new Action(DoSomething));

        this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MainViewModel_PropertyChanged);

    }

    void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        MessageBox.Show(e.PropertyName);
    }


}v

I am trying to get a sample to work using MVVM Light and the Messaging Class. In the sample, I have a test project created from the MVVM Template for Silveright 4. I have added a button on the main page. When the button is clicked, it updates a property on the ViewModel. When the property is updated, I want to show a messagebox with the new value.

The key line of code is:

Messenger.Default.Register(this, new Action(ShowMessage));

I can get this to work in WPF, but not silverlight. It should call ShowMessage with the string parameter when the property changes, but it does not. If I use:

Messenger.Default.Send("Hello MVVM");

This works and the string is sent as a message to ShowMessage.

However, the message does not get sent if the property changes, even though the property was created with the MVVMINPC snippet and has the following line:

RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);

This should have the same effect as Messager.Default.Send but it seems to be ignored. ThePropertyChangedEvent is indeed raised, but the messanger part seems to be disconnected.

Am I doing something wrong? Here is the full MainViewModel:

public class MainViewModel : ViewModelBase
{
    public RelayCommand MyRelayCommand { get; set; }
    public const string MyPropertyPropertyName = "MyProperty";
    private string _myProperty = "test";

    public string MyProperty
    {
        get
        {
            return _myProperty;
        }

        set
        {
            if (_myProperty == value)
            {
                return;
            }

            var oldValue = _myProperty;
            _myProperty = value;
            RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);
        }
    }

    public void DoSomething()
    {
        //Messenger.Default.Send("Hello MVVM"); //Works
        this.MyProperty = "Hello World"; //Doesn't work.
    }



    public void ShowMessage(string message)
    {
        MessageBox.Show(message);
    }

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {

        Messenger.Default.Register(this, new Action<string>(ShowMessage));

        MyRelayCommand = new RelayCommand(new Action(DoSomething));

        this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MainViewModel_PropertyChanged);

    }

    void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        MessageBox.Show(e.PropertyName);
    }


}public class MainViewModel : ViewModelBase
{
    public RelayCommand MyRelayCommand { get; set; }
    public const string MyPropertyPropertyName = "MyProperty";
    private string _myProperty = "test";

    public string MyProperty
    {
        get
        {
            return _myProperty;
        }

        set
        {
            if (_myProperty == value)
            {
                return;
            }

            var oldValue = _myProperty;
            _myProperty = value;
            RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);
        }
    }

    public void DoSomething()
    {
        //Messenger.Default.Send("Hello MVVM"); //Works
        this.MyProperty = "Hello World"; //Doesn't work.
    }



    public void ShowMessage(string message)
    {
        MessageBox.Show(message);
    }

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {

        Messenger.Default.Register(this, new Action<string>(ShowMessage));

        MyRelayCommand = new RelayCommand(new Action(DoSomething));

        this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MainViewModel_PropertyChanged);

    }

    void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        MessageBox.Show(e.PropertyName);
    }


}v

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2024-09-22 08:41:00

好的,我发现 Register 行应该如下所示:

Messenger.Default.Register(this, new Action<PropertyChangedMessage<string>>(ShowMessage));

重点是有不同类型的消息,并且您必须注册 PropertyChangedMessage 类型才能接收属性更改消息。

然后,接收消息的 Action 需要采用正确的参数,如下所示:

public void ShowMessage(PropertyChangedMessage<string> e)
    {
        MessageBox.Show(e.NewValue.ToString());
    }

OK, I found that the Register line should look like this:

Messenger.Default.Register(this, new Action<PropertyChangedMessage<string>>(ShowMessage));

The point being there are different types of messages, and you have to register the PropertyChangedMessage type to recieve property changed messages.

Then also, the Action that recieves the message needs to take the correct parameter, like this:

public void ShowMessage(PropertyChangedMessage<string> e)
    {
        MessageBox.Show(e.NewValue.ToString());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文