2012-09-06 15 views
5

Ho bisogno di eseguire un'operazione di cancellazione asincrona con conferma utente. Qualcosa del genere:Esecuzione comando asincrona con conferma utente

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

Il problema è che MessageBox esegue anche in modo asincrono. Qual è lo schema migliore in ReactiveUI per chiedere all'utente in modo sincrono e quindi eseguire il metodo in modo asincrono?

risposta

6

Il modo più semplice per farlo è quello di utilizzare solo due comandi:

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