我有问题,我可以按照本教程了解我在这个链接上学习WPF:https://www.tutorialspoint.com/mvvm/mvvm_view_viewmodel_communication.htm,但是当我执行时,他会返回错误:
“错误1”MVVMDemo.MyICommand'未实现接口成员'System.Windows.Input.ICommand.CanExecuteChanged'C:\ Users \ Adriano \ documents \ visual studio 2013 \ Projects \ MVVMDemo \ MVVMDemo \ MyICommand.cs 10 11 MVVMDemo“
我不明白问题出在哪里......
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();
}
}
}
}
停在
public MainWindow()
{
InitializeComponent();
}
错误'MVVMDemo.exe中发生了'System.NotImplementedException'类型的异常但未在用户代码'
中处理
答案 0 :(得分:0)
将公开明确添加到事件处理程序
public event EventHandler CanExecuteChanged = delegate { };
完整的解决方案发布在Github repo https://github.com/vjoks/WPF-MVVM/tree/master/MVVMHierarchiesDemo-pre-Validation
上