【发布时间】:2017-03-30 10:57:46
【问题描述】:
TL;DR是否可以将异步可取消命令附加到包含孩子的Parent-View-Model
我的问题是,我有一个View 的ViewModel,它显示一个“WizardControl”(就像一个安装助手,您可以点击下一步进入下一个视图)
Log in 按钮是Parent-View-Model 的一部分,为此按钮执行的命令必须是来自Specific Page(又名子)和MainWindow(又名父母)。
现在我在父命令中执行子命令:
ForwardCommand = new DelegateCommand(() =>
{
CancelEventArgs cea = new CancelEventArgs();
if (NextCommand != null)
{
NextCommand.Execute(null, cea));
if (!cea.Cancel)
{
//Go to next page
}
//Else stay on that page because an error occured while the command
}
},
(nul)=> {
if (NextCommand != null)
{
return true;
}
return NextCommand.CanExecute(nul);
});
这是可行的,只要包含命令不需要异步执行,Login 就是这种情况。因为随后会显示下一页,而无需用户登录以获取下一页所需的数据。 (登录时第一页会显示一个加载栏)。对于执行 Child-Command 时发生错误的情况,我实现了 CancelEventArgs,如您在代码中看到的那样。
提前致谢,
弗洛尔
PS。 The DelegateCommand 是由CancellationEventArgs 扩展的自制实现
编辑:
我在Attached Commands 之前的实现是我在页面上有一个额外的“登录”按钮可以点击,并且只有在用户登录时才允许进入下一步,但这是糟糕的用户体验
【问题讨论】: