如何将MVVM-Light与令牌一起使用? [英] How to use MVVM-Light with tokens?

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

问题描述

我在MVVM-Light软件包中看到,我可以发送带有令牌的消息-我需要做的就是发送一个对象,并在该对象上附加一条消息-例如添加,编辑,删除任何内容.

I see in the MVVM-Light package that I can send messages with tokens- what I need to do is send an object, with a message attached to that object- like Add, Edit, Delete whatever.

发送和接收此消息的最佳方法是什么?我认为对于发送其公正: Messenger.Default.Send(myObject,ActionEnum.DELETE);

What is the best way to send and to recieve this message? I think for send its just: Messenger.Default.Send(myObject, ActionEnum.DELETE);

但是在接收中: Messenger.Default.Register(this,????,HandleMyMessage);

But in the receive: Messenger.Default.Register(this, ????, HandleMyMessage);

正确的语法是什么?

谢谢!

推荐答案

这里是发送和注册代码的快速部分.您的通知是指示接收者意图的消息. 内容"是您要发送的项目,您可以进一步确定发送消息的人,甚至是发件人和目标发送给该对象的对象.

Here is a quick section of code for both the send and the register. Your Notification is the message that instructs the receiver what the intention was. The Content is the item you wanted to send, and you can further identify who sent the message, and even what object this message was intended for with the sender and the target.

Messenger.Default.Send<NotificationMessage<Job>>(
    new NotificationMessage<Job>(this, myJob, "Add")
);

Messenger.Default.Register<NotificationMessage<Job>>(
this, nm =>

{
    // this might be a good idea if you have multiple recipients.
    if (nm.Target != null &&
        nm.Target != this)
        return;

    // This is also an option 
    if (nm.Sender != null &&
        nm.Sender != expectedFrom) // expectedFrom is the object whose code called Send
        return;

    // Processing the Message
    switch(nm.Notification)
    {
        case "Add":
            Job receivedJob = nm.Content;
            // Do something with receivedJob
            break;
        case "Delete":
            Job receivedJob = nm.Content;
            // Do something with receivedJob
            break;
    }
});

这篇关于如何将MVVM-Light与令牌一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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