找不到RelayCommand的参考

时间:2014-01-28 12:39:56

标签: c# wpf mvvm command relaycommand

我有一个WPF应用程序,其中我想将其设计模式更改为MVVM。我使用了此代码段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FirstMVVm.Model;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;
namespace FirstMVVm.ModelView
{
    class MyViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private float result;

          public float Result
          {
           get { return result; }
              private set
              {
                  if (result != value) { 
                      result = value;
                  if (PropertyChanged != null)
                  {
                      PropertyChanged(this, new PropertyChangedEventArgs("Result"));
                  }
                     }
                    }
                 }

          public int Number { get; set; }

        private RelayCommand _calculatePerimeterCommand;

        public ICommand CalculatePerimeterCommand
                  {
                  get
                    {
                    if (_calculatePerimeterCommand == null)
                      {
                          _calculatePerimeterCommand = new RelayCommand(param => this.CalculatePerimeter());
                       }
                    return _calculatePerimeterCommand;
                       }
                      }
        private MyModel _model;

        public MyViewModel() {
            _model = new MyModel();
        }


        private void CalculatePerimeter(){
            Result = _model.Perimetre(Number);
        }

  }
}

问题是RelayCommand类型未知,我不知道装配缺失的是什么。

  • 那么我该如何解决这个问题?

谢谢,

2 个答案:

答案 0 :(得分:8)

RelayCommand是MS创建的用于处理WPF中的事件或命令的类。你可以创建自己的类或通过以下链接。

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

答案 1 :(得分:1)

这不一定是RelayCommand,它只是网上最常见示例的类名。

ICommand的机制以某种方式工作,而不是调用getter或setter,而是在实现public void Execute(object parameter)的类上调用ICommand

让我举个例子:

我有一个超链接,在点击之前应该做一些事情,然后再将人重定向到浏览器。

<强> XAML

<Hyperlink NavigateUri="https://payments.epdq.co.uk/ncol/prod/backoffice/"
     Command="{Binding Path=NavigateToTakePayment}"  IsEnabled="{Binding CanTakePayment}">
     Launch Payments Portal
</Hyperlink>

现在在viewModel中我有属性

public ICommand NavigateToTakePayment       
{
    get { return _navigateToTakePayment ?? (_navigateToTakePayment = new NavigateToTakePaymentCommand(this)); }
    set { _navigateToTakePayment = value; }
}

但是当点击超链接而不是像预期NavigateToTakePaymentCommandExecute方法那样被触发时,会触发getter。

  public class NavigateToTakePaymentCommand : ICommand
  {
        public NavigateToTakePaymentCommand(PaymentViewModel paymentViewModel)
        {
            ViewModel = paymentViewModel;
        }

        public PaymentViewModel ViewModel { get; set; }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
         //your implementation stuff goes here.
        }

        public event EventHandler CanExecuteChanged;
  }

我希望这个例子能说明这个机制是如何运作的,并且会为你节省一些时间。