【发布时间】:2018-03-01 16:24:11
【问题描述】:
我正在使用 MVVMLight 框架在 WPF 中开发应用程序。
我正在尝试进行单元测试(我是新手)。所以我尝试通过订阅我的命令上的 CanExecuteChanged 事件来模拟我的视图,并验证它是否被正确调用。但是当我这样做时,它永远不会被调用,即使我调用了 RaiseCanExecuteChanged 方法。
这是一个非常简单的示例:
bool testCanExec = false;
var testCmd = new RelayCommand(
execute: () => { System.Diagnostics.Debug.WriteLine($"Execute call"); },
canExecute: () => { System.Diagnostics.Debug.WriteLine($"CanExecute call"); return testCanExec; }
);
testCmd.CanExecuteChanged += ((sender, args) => { System.Diagnostics.Debug.WriteLine($"CanExecuteChanged call"); });
testCanExec = true;
testCmd.RaiseCanExecuteChanged(); // <= nothing in output
testCmd.Execute(null); // <= output: "CanExecute call", "Execute call"
我真正无法理解的是,它似乎可以与我的按钮一起使用。我不知道如何,但它可以正确启用和禁用。
感谢您的帮助。
【问题讨论】:
标签: c# wpf mvvm-light relaycommand