6

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

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 中同步询问用户然后异步执行方法的最佳模式是什么?

4

1 回答 1

6

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

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);
于 2012-09-07T05:42:15.710 回答