我在MVVM-Light包中看到我可以用令牌发送消息 - 我需要做的是发送一个对象,并在该对象上附加一条消息,如添加,编辑,删除等等.
发送和接收此消息的最佳方式是什么?我认为发送它只是:Messenger.Default.Send(myObject,ActionEnum.DELETE);
但在接收:Messenger.Default.Register(this,????,HandleMyMessage);
什么是正确的语法?
谢谢!
LBu*_*ion 14
同样作为补充:令牌并不意味着识别任务(通知),而是识别接收器.注册者使用与发送者相同的令牌来接收消息,而所有其他接收者都不会得到消息.
对于您想要做的事情,我使用工具包中包含的可选NotificationMessage类型.它有一个额外的字符串属性(通知),您可以将其设置为您想要的任何内容.我用它来"接受命令"给接收者.
干杯,洛朗
Rya*_*ver 13
这是发送和寄存器的快速代码段.您的通知是指示接收者意图是什么的消息.内容是您要发送的项目,您可以进一步确定发送邮件的人员,甚至是邮件与发件人和目标的对象.
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;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6656 次 |
| 最近记录: |