RelayCommand link not found

I have a WPF application in which I would like to change its design template to MVVM. I 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 type is RelayCommandunknown, and I do not know what is missing in the assembly.

  • So how can I fix this problem?

Thanks,

+4
source share
2 answers

RelayCommand is a class created by MS to handle an event or command in WPF. You can create your own class or follow the link below.

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

+7
source

RelayCommand .

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

, getter , , NavigateToTakePaymentCommand class Execute.

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

, , .

+1

Source: https://habr.com/ru/post/1524065/


All Articles