2012-09-06 75 views
5

我需要使用用戶確認執行異步刪除操作。類似這樣的:使用用戶確認執行異步命令

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中最好的模式來同步詢問用戶,然後異步執行方法?

回答

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