如何使用RelayCommand与MVVM光框架 [英] How to use RelayCommand with the MVVM Light framework

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

问题描述

我刚刚开始学习MVVM轻型框架,我无法找到如何使用RelayCommand任何简单的例子。对于学习的目的,我只是想有在我看来,一个按钮,点击时显示是一个Hello World World消息框,这是每分钟甚至启用(基本上如果DateTime.Now.Minute%2 == 0)

I've just started learning the MVVM Light framework and I can't find any straightforward examples on how to use a 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).

如何将按钮XAML的外观和?

How would the button XAML look and how would the RelayCommand HelloWorld be defined in the ViewModel?

由于会如何RelayCommand的HelloWorld在视图模型中定义您的帮助!

Thanks for your help!!

推荐答案

RelayCommand 的目的是落实在的ICommand 界面,按钮控制的需求,并只通过电话到一般坐在旁边他们在视图模型的一些其他功能。

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.

因此,例如,你将有一个ViewModel类,如:

So for example, you would have a ViewModel class like:

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
    }
}

在你的控制,你将有的DataContext 设置无论是在直接通过的DataContext = {StaticResource的...}

In your control you would have the DataContext set either in the code behind or in the XAML directly through a DataContext={StaticResource ...}

然后,您的按钮将绑定到命令,在视图模型像这样

Your button would then bind to the command in the ViewModel like so

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

在单击该按钮,它使用 DisplayMessageCommand 键,通话执行()这个对象上 RelayCommand 只是转发到 DisplayMessage 方法。

When the Button is clicked, it uses the DisplayMessageCommand and calls Execute() on this object which RelayCommand just forwards onto the DisplayMessage method.

DispatchTimer 熄灭一分钟一次,并呼吁 RaiseCanExecuteChanged()。这使得这势必命令来重新检查命令仍然有效或无效的按钮。否则,你可能只按一下按钮,找出该命令当前不可用。

The DispatchTimer goes off once a minute and calls 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.

这篇关于如何使用RelayCommand与MVVM光框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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