通过 MVVMLight 发送命令

发布于 2024-12-05 03:31:37 字数 674 浏览 1 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

怪我入戏太深 2024-12-12 03:31:37

不太确定您想要实现什么目标,但我想您想将消息从一个视图发送到另一个视图。在这种情况下,您使用

Messenger.Send(Message.Reset);

来发送消息。在收件人方面,您使用以下代码:

Messenger.Register<MessageOp>(this, (m) => {
    if (m == MessageOp.Reset) {
        // your code
    }
});

非常重要:如果您使用消息传递,请务必从 Messenger 中删除收件人。对于视图模型,这可以通过在视图模型上调用 Cleanup 来完成。在所有其他情况下,请使用 Messenger.Unregister(recipient)。这是必要的,因为 MVVM Light 中的弱操作实现存在一个已知的问题,即自行释放接收者。

但是,如果您只想将按钮(或类似的东西)绑定到命令,则可以使用 RelayCommand。

将以下定义添加到您的视图模型中:

public RelayCommand ResetCommand {
    get {
        return _resetCommand ?? (_resetCommand = new RelayCommand(
            () => { 
                // your execution code
            },
            () => {
                // can execute - gets called quite often!
            )
        ));
    }
}
private RelayCommand _resetCommand;

然后您可以将此命令绑定到按钮

<button Content="Reset" Command="{Binding ResetCommand}"/>

编辑

要向特定收件人发送消息,实际上有两种可能性:

  1. 在发送消息时添加令牌。
  2. 创建只有收件人订阅的自定义消息。

就我个人而言,我会选择第二种方法,因为它更清晰、更明确,因此更易于维护。因此,要创建自定义消息,您可以执行以下操作:

public class OperationMessage : GenericMessage<MessageOp> {
    public OperationMessage(MessageOp operation) : base(operation) { }
}

public class ResetMessage : OperationMessage 
{
    public ResetMessage() : base(MessageOp.Reset) { }
}

现在您可以发送

Messenger.Send(new ResetMessage());

和接收

Messenger.Register<ResetMessage>(this, (m) => {
    // do your resetting here
});

Messenger.Register<OperationMessage>(this, true, (m) => {
    // handle all operations here - the operation is avaiable via m.Content
});

我创建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

Messenger.Send(Message.Reset);

to send the message. On the recipient side you use the following code:

Messenger.Register<MessageOp>(this, (m) => {
    if (m == MessageOp.Reset) {
        // your 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:

public RelayCommand ResetCommand {
    get {
        return _resetCommand ?? (_resetCommand = new RelayCommand(
            () => { 
                // your execution code
            },
            () => {
                // can execute - gets called quite often!
            )
        ));
    }
}
private RelayCommand _resetCommand;

Then you can bind this command to a button

<button Content="Reset" Command="{Binding ResetCommand}"/>

Edit

To send a message to a specific recipient, there are really two possibilities:

  1. Add a token when you are sending the message.
  2. Create a custom message that only the recipient subscribes to.

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:

public class OperationMessage : GenericMessage<MessageOp> {
    public OperationMessage(MessageOp operation) : base(operation) { }
}

public class ResetMessage : OperationMessage 
{
    public ResetMessage() : base(MessageOp.Reset) { }
}

Now you can send

Messenger.Send(new ResetMessage());

and receive

Messenger.Register<ResetMessage>(this, (m) => {
    // do your resetting here
});

or

Messenger.Register<OperationMessage>(this, true, (m) => {
    // handle all operations here - the operation is avaiable via m.Content
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文