wpf 找不到 RelayCommand 的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21405792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:38:02  来源:igfitidea点击:

RelayCommand's reference is not found

c#wpfmvvmcommandrelaycommand

提问by Lamloumi Afif

I have a WPF application in which i'd like to change its design pattern to MVVM.I have used this snippet

我有一个 WPF 应用程序,我想在其中将其设计模式更改为MVVM.I have used this snippet

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

  }
}

The problem is that the RelayCommandtype is not known and i don't know what is the assembly missing.

问题是RelayCommand类型未知,我不知道缺少什么程序集。

  • So how can i fix this problem?
  • 那么我该如何解决这个问题呢?

Thanks,

谢谢,

回答by J R B

RelayCommand is a class created by MS for handling event or command in WPF. you can create own class or go through below link.

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

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

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

回答by Matas Vaitkevicius

This does not necessarily have to be RelayCommandit's simply a name of class from most common example on the web.

这不一定RelayCommand是网络上最常见示例中的类名。

The mechanism of ICommandworks in a way that instead of getter or setter being called you get public void Execute(object parameter)to be called on class that is implementing ICommand

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

Let me give you an example:

让我给你举个例子:

I have a hyperlink which when clicked should do some stuff before redirecting person to browser.

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

XAML

XAML

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

now in viewModel I have property

现在在 viewModel 我有财产

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

but when hyperlink is clicked instead of getter being fired like one would expect NavigateToTakePaymentCommandclass Executemethod is fired instead.

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

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

I hope this example will clarify how the mechanism works and will save you some time.

我希望这个例子能够阐明该机制的工作原理并为您节省一些时间。