【问题标题】:Asynchronous command execution with user confirmation带用户确认的异步命令执行
【发布时间】:2012-09-06 10:10:06
【问题描述】:

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

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

【问题讨论】:

    标签: c# .net mvvm system.reactive reactiveui


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-22
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多