简体   繁体   English

如何将 RelayCommand 与 MVVM Light 框架一起使用

[英]How to use RelayCommand with the MVVM Light framework

I've just started learning the MVVM Light framework and I can't find any straightforward examples on how to use a RelayCommand.我刚刚开始学习 MVVM Light 框架,我找不到任何关于如何使用 RelayCommand 的简单示例。 For purposes of learning, I'd just like to have a button in my view which when clicked show's a hello world world message box, and which is enabled on every even minute (basically if DateTime.Now.Minute % 2 == 0).出于学习的目的,我只想在我的视图中有一个按钮,单击该按钮时显示的是一个 hello world 消息框,并且每隔一分钟启用(基本上如果 DateTime.Now.Minute % 2 == 0) .

How would the button XAML look and how would the RelayCommand HelloWorld be defined in the ViewModel?按钮 XAML 的外观如何以及如何在 ViewModel 中定义 RelayCommand HelloWorld?

Thanks for your help!!谢谢你的帮助!!

RelayCommand 's purpose is to implement the ICommand interface that Button controls needs and to just pass the calls onto some other function which generally sits right next to them in the ViewModel. RelayCommand的目的是实现 Button 控件所需的ICommand接口,并将调用传递给其他一些 function,它们通常位于 ViewModel 中它们旁边。

So for example, you would have a ViewModel class like:因此,例如,您将有一个 ViewModel class ,例如:

class HelloWorldViewModel : ViewModelBase
{
    public RelayCommand DisplayMessageCommand { get; private set; }

    private DispatchTimer _timer;

    public HelloWorldViewModel()
    {
        this.DisplayMessageCommand = new RelayCommand(this.DisplayMessage, CanDisplayMessage);

        // Create a timer to go off once a minute to call RaiseCanExecuteChanged
        _timer = new DispatchTimer();
        _timer = dispatcherTimer.Tick += OnTimerTick;
        _timer.Interval = new Timespan(0, 1, 0);
        _timer.Start();
    }

    private void OnTimerTick(object sender, EventArgs e)
    {
        this.DisplayMessageCommand.RaiseCanExecuteChanged();
    }

    public bool CanDisplayMessage()
    {
        return DateTime.Now.Minute % 2 == 0;
    }

    public void DisplayMessage()
    {
        //TODO: Do code here to display your message to the user
    }
}

In your control you would have the DataContext set either in the code behind or in the XAML directly through a DataContext={StaticResource...}在您的控制中,您可以直接通过DataContext DataContext={StaticResource...}在代码后面或 XAML 中设置 DataContext

Your button would then bind to the command in the ViewModel like so然后,您的按钮将绑定到 ViewModel 中的命令,如下所示

<Button Content='Push me' Command='{Binding DisplayMessageCommand}' />

When the Button is clicked, it uses the DisplayMessageCommand and calls Execute() on this object which RelayCommand just forwards onto the DisplayMessage method.单击 Button 时,它使用DisplayMessageCommand并在此 object 上调用Execute()RelayCommand将其转发到DisplayMessage方法。

The DispatchTimer goes off once a minute and calls RaiseCanExecuteChanged() . DispatchTimer每分钟关闭一次并调用RaiseCanExecuteChanged() This allows the button which is bound to the command to re-check if the command is still valid or not.这允许绑定到命令的按钮重新检查命令是否仍然有效。 Otherwise, you might click the button only to find out that the command isn't currently available.否则,您可能会单击该按钮而发现该命令当前不可用。

Or with lambda或与 lambda

    private RelayCommand<anyobject> _AddCmd;
    public ICommand AddPoint
    {
        get
        {
            return _AddCmd ??
                (
                _AddCmd = new RelayCommand
                    (
                        (obj) =>
                        {
                            ViewModelWF.ZeroPoints.Add(new WM.Point(0, 0));
                        }
                    )
                );
        }
    }

    private RelayCommand _DeleteCmd;
    public ICommand DeletePoint
    {
        get
        {
            return _DeleteCmd ??
                (
                _DeleteCmd = new RelayCommand
                    (
                        () =>
                        {
                            int idx = wpfZeroPoints.SelectedIndex;
                        },
                        () =>
                        {
                            return wpfZeroPoints.SelectedIndex <= 0;
                        }
                    )
                );
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM