具有用户确认的异步命令执行

时间:2021-01-09 15:52:35

I need to execute async delete operation with user confirmation. Something like this:

我需要执行带有用户确认的异步删除操作。是这样的:

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
    }
}

The problem is the MessageBox would execute asynchronously too. Which is the best pattern in ReactiveUI to ask user synchronously and then execute method asynchronously?

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

1 个解决方案

#1


6  

The most straightforward way to do this is to just use two commands:

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

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);

#1


6  

The most straightforward way to do this is to just use two commands:

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

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);