2016-09-29 2 views
0

Ich verwende eine vorcodierte AsynchronousCommand Referenzklasse meine Methode aufzurufen, und ich möchte die gleiche Referenz verwenden diese Methode abzubrechen, weil diese Referenzklasse bereits habe einige Methoden zum Abbrechen. Sie können diese Klassen im folgenden Link siehe auch: http://pastebin.com/00eStgP6Taste, um ein Async-Methode auf Abbrechen (WPF - Datenbindung comand)

public class AsynchronousCommand : CommandReference, INotifyPropertyChanged 
    { 
     public AsynchronousCommand(Action action, bool canExecute = true) 
      : base(action, canExecute) 
     { 
      Initialise(); 
     } 

     public AsynchronousCommand(Action<object> parameterizedAction, bool canExecute = true) 
      : base(parameterizedAction, canExecute) 
     { 
      Initialise(); 
     } 

     private void Initialise() 
     { 
      cancelCommand = new CommandReference(
     () => 
      { 
       IsCancellationRequested = true; 
      }, true); 
     } 

     public override void DoExecute(object param) 
     { 
      if (IsExecuting) 
       return; 

      CancelCommandEventArgs args = new CancelCommandEventArgs() { Parameter = param, Cancel = false }; 
      InvokeExecuting(args); 

      if (args.Cancel) 
       return; 

      IsExecuting = true; 

      callingDispatcher = Dispatcher.CurrentDispatcher; 

      ThreadPool.QueueUserWorkItem(
      (state) => 
      { 
       InvokeAction(param); 

       ReportProgress(
       () => 
        { 
         IsExecuting = false; 

         if (IsCancellationRequested) 
          InvokeCancelled(new CommandEventArgs() { Parameter = param }); 
         else 
          InvokeExecuted(new CommandEventArgs() { Parameter = param }); 

         IsCancellationRequested = false; 
        } 
       ); 
      } 
     ); 
     } 

     private void NotifyPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler propertyChanged = PropertyChanged; 

      if (propertyChanged != null) 
       propertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     public void ReportProgress(Action action) 
     { 
      if (IsExecuting) 
      { 
       if (callingDispatcher.CheckAccess()) 
        action(); 
       else 
        callingDispatcher.BeginInvoke(((Action)(() => { action(); }))); 
      } 
     } 

     public bool CancelIfRequested() 
     { 
      if (IsCancellationRequested == false) 
       return false; 

      return true; 
     } 

     protected void InvokeCancelled(CommandEventArgs args) 
     { 
      CommandEventHandler cancelled = Cancelled; 

      if (cancelled != null) 
       cancelled(this, args); 
     } 

     protected Dispatcher callingDispatcher; 

     private bool isExecuting = false; 

     private bool isCancellationRequested; 

     private CommandReference cancelCommand; 

     public event PropertyChangedEventHandler PropertyChanged; 

     public event CommandEventHandler Cancelled; 

     public bool IsExecuting 
     { 
      get 
      { 
       return isExecuting; 
      } 
      set 
      { 
       if (isExecuting != value) 
       { 
        isExecuting = value; 
        NotifyPropertyChanged("IsExecuting"); 
       } 
      } 
     } 

     public bool IsCancellationRequested 
     { 
      get 
      { 
       return isCancellationRequested; 
      } 
      set 
      { 
       if (isCancellationRequested != value) 
       { 
        isCancellationRequested = value; 
        NotifyPropertyChanged("IsCancellationRequested"); 
       } 
      } 
     } 

     public CommandReference CancelCommand 
     { 
      get { return cancelCommand; } 
     } 
    } 

ich in der Ansicht den Befehl bin verbindlich.

Command="{Binding ComandoProcessarArquivo} 

Und in meinem Viewmodel:

private AsynchronousCommand _comandoProcessarArquivo; 

public ICommand ComandoProcessarArquivo 
     { 
      get { return _comandoProcessarArquivo ??  (_comandoProcessarArquivo = new AsynchronousCommand(new Action(() => ProcessarArquivo()))); } 
     } 

private void ProcessarArquivo() 
     { 
    new ProcessarArquivo().Iniciar(ArquivoOrigem, ArquivoDestino, AtualizarEtapaProcesso); 
} 

Also habe ich versucht, die für mehr als 2 Stunden Abbrechen-Button zu erstellen, kann mir bitte jemand ein Licht geben: |

Danke!

Antwort

0

habe ich diese besondere Async Befehl Umsetzung noch nicht gesehen, und es scheint ziemlich verworren zu sein.

Das heißt, macht es ein zweites CancelCommand. Sie haben nicht XAML für die Löschtaste umfassen, aber die Bindung sollte wie etwas aussehen:

Command="{Binding ComandoProcessarArquivo.CancelCommand}" 

Wenn dieser Befehl Implementierung ist etwas, das Sie (und nicht als ein Unternehmen Standard) gefunden, dann würde ich nach anderen Optionen suchen um empfehlen. Nito.AsyncEx hat eine gute moderne Async-Befehlsimplementierung.

+0

Andrew, ich arbeite an einem Unternehmen Software und sie verwendet diese Klasse, aber ich bin zu ändern frei. Ich habe versucht, bevor die cancelcommand zu implementieren, wie Sie gesagt haben, aber den Ball hielt es Arbeit :(Ich werde einen Blick auf nito.asyncex geben so schnell wie möglich, haben Sie einen Link Danke paaren.? P –