0

im with problems, im follow this tutorial where im learn about WPF on this link: https://www.tutorialspoint.com/mvvm/mvvm_view_viewmodel_communication.htm, but when im execute, he return error:

"Error 1 'MVVMDemo.MyICommand' does not implement interface member 'System.Windows.Input.ICommand.CanExecuteChanged' C:\Users\Adriano\documents\visual studio 2013\Projects\MVVMDemo\MVVMDemo\MyICommand.cs 10 11 MVVMDemo"

I dont understand where is the problem...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MVVMDemo
{
    class MyICommand : ICommand
    {
        Action _TargetExecuteMethod; 
      Func<bool> _TargetCanExecuteMethod;

      public MyICommand(Action executeMethod) {
         _TargetExecuteMethod = executeMethod; 
      }

      public MyICommand(Action executeMethod, Func<bool> canExecuteMethod){ 
         _TargetExecuteMethod = executeMethod;
         _TargetCanExecuteMethod = canExecuteMethod; 
      }

      public void RaiseCanExecuteChanged() { 
         CanExecuteChanged(this, EventArgs.Empty); 
      }

      bool ICommand.CanExecute(object parameter) { 

         if (_TargetCanExecuteMethod != null) { 
            return _TargetCanExecuteMethod(); 
         } 

         if (_TargetExecuteMethod != null) { 
            return true; 
         } 

         return false; 
      }

      // Beware - should use weak references if command instance lifetime 
         //is longer than lifetime of UI objects that get hooked up to command 

      // Prism commands solve this in their implementation public event 
      EventHandler CanExecuteChanged = delegate { };

      void ICommand.Execute(object parameter) { 
         if (_TargetExecuteMethod != null) {
            _TargetExecuteMethod(); 
         } 
      } 
    }
}

It stops in

public MainWindow()
        {
            InitializeComponent();
        }

error 'An exception of type 'System.NotImplementedException' occurred in MVVMDemo.exe but was not handled in user code'

il_raffa
  • 5,090
  • 129
  • 31
  • 36

1 Answers1

0

Explicitly add public to event handler

public event EventHandler CanExecuteChanged = delegate { };

full solution posted on Github repo https://github.com/vjoks/WPF-MVVM/tree/master/MVVMHierarchiesDemo-pre-Validation

vicport
  • 1,094
  • 1
  • 8
  • 6