gpt4 book ai didi

c# - 用户确认的异步命令执行

转载 作者:太空狗 更新时间:2023-10-29 23:09:21 26 4
gpt4 key购买 nike

我需要在用户确认的情况下执行异步删除操作。像这样:

public ReactiveAsyncCommand DeleteCommand { get; protected set; }
...
DeleteCommand = new ReactiveAsyncCommand();
DeleteCommand.RegisterAsyncAction(DeleteEntity);

...

private void DeleteEntity(object obj)
{
if (MessageBox.Show("Do you really want to delete this entity?", "Confirm", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
//some delete operations
}
}

问题是 MessageBox 也会异步执行。ReactiveUI 中同步询问用户然后异步执行方法的最佳模式是什么?

最佳答案

最直接的方法是只使用两个命令:

public ReactiveCommand DeleteCommand { get; protected set; }
private ReactiveAsyncCommand ExecuteDelete { get; protected set; }

/*
* In the Constructor
*/

ExecuteDelete = new ReactiveAsyncCommand();
ExecuteDelete.RegisterAsyncAction(() => /* Do the delete */);

DeleteCommand = new ReactiveCommand(ExecuteDelete.CanExecuteObservable);
DeleteCommand
.Where(_ => MessageBox.Show("Delete?") == MessageBoxResult.Yes)
.InvokeCommand(ExecuteDelete);

关于c# - 用户确认的异步命令执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12297729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com