通过 MVVMLight 发送命令
我正在使用 MVVMLight 并经历了一些学习曲线来尝试弄清楚通信是如何工作的。我了解属性如何与 Silverlight 绑定一起工作,这一切都非常简单。
现在我想知道什么是作为命令发送消息的最佳方法。例如,我想向我的 ViewModel 发送一条消息以删除其列表。
所以我可以创建一个枚举来充当命令
enum MessageOp
{
Reset
}
但是我怎样才能将其作为命令发送呢?目前我将其作为课程发送,但随后需要再次将其作为令牌发送。像这样:
Messenger.Default.Send<MessageOp>(MessageOp.Reset, MessageOp.Reset);
我在 ViewModel 中的代码如下:
Messenger.Default.Register<MessageOp>(
this, MessageOp.Reset,
delegate(MessageOp op)
{
// Erase all entries
MyDictionary.Clear();
});
我想到的一种方法是创建一个完全空的类来充当命令。这是最好的方法吗,因为我觉得创建空类来这样做有点奇怪
I am using MVVMLight and going through a little of a learning curve to try and work out how communication works. I understand how Properties work together with Silverlight binding and that's all quite straight-forward.
Now what I want to know is what is the best approach for sending a Message as a Command. So for example I want to send a message to my ViewModel to erase its list.
So I could create an enum to act as a command
enum MessageOp
{
Reset
}
But then how could I send this as a command? At the moment I send it as the class, but then need to send it again as the token. Like so:
Messenger.Default.Send<MessageOp>(MessageOp.Reset, MessageOp.Reset);
With my code in the ViewModel like this:
Messenger.Default.Register<MessageOp>(
this, MessageOp.Reset,
delegate(MessageOp op)
{
// Erase all entries
MyDictionary.Clear();
});
One way I thought of was creating a totally empty class to act as a command. Would this be the best approach as I feel a bit odd creating empty classes to act like this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不太确定您想要实现什么目标,但我想您想将消息从一个视图发送到另一个视图。在这种情况下,您使用
来发送消息。在收件人方面,您使用以下代码:
非常重要:如果您使用消息传递,请务必从 Messenger 中删除收件人。对于视图模型,这可以通过在视图模型上调用 Cleanup 来完成。在所有其他情况下,请使用 Messenger.Unregister(recipient)。这是必要的,因为 MVVM Light 中的弱操作实现存在一个已知的问题,即自行释放接收者。
但是,如果您只想将按钮(或类似的东西)绑定到命令,则可以使用 RelayCommand。
将以下定义添加到您的视图模型中:
然后您可以将此命令绑定到按钮
编辑
要向特定收件人发送消息,实际上有两种可能性:
就我个人而言,我会选择第二种方法,因为它更清晰、更明确,因此更易于维护。因此,要创建自定义消息,您可以执行以下操作:
现在您可以发送
和接收
或
我创建OperationMessage 的原因是它更灵活,并且允许您根据需要一般或特定地处理您的操作。
Not quite sure what you trying to achive, but I guess you want to send a message from one view to another. In this case you use
to send the message. On the recipient side you use the following code:
Very important: If you use messaging be sure to remove the recipient from the Messenger. In case of a view model this can be done by calling Cleanup on the view model. In all other cases use Messenger.Unregister(recipient). This is necessary as the weak action implementation in MVVM Light has a known problem releasing the recipient on its own.
However, if you just wanted to bind a button (or something similar) to a command, you use a RelayCommand.
Add the following definition to your view model:
Then you can bind this command to a button
Edit
To send a message to a specific recipient, there are really two possibilities:
Personally, I'd go for the second approach as it is more clear and explicit - and therefore more maintainable. So, to create a custom Message you can do the following:
Now you can send
and receive
or
The reason why I'd create the OperationMessage is that it is more flexible, and lets you handle your operations genericly or specificly, as need be.