2016-09-29 40 views
0

我正在使用預編碼的AsynchronousCommand引用類來調用我的方法,我想使用相同的引用來取消該方法,因爲此引用類已經有一些方法可以取消。 您可以在以下網站也看到這個類: http://pastebin.com/00eStgP6取消異步方法的按鈕(wpf - databinding命令)

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

我在視圖結合的命令。

Command="{Binding ComandoProcessarArquivo} 

而且在我的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); 
} 

所以,我試圖創建2個多小時的取消按鈕,可以請別人給我淡淡的道:|

謝謝!

回答

0

我還沒有看過這個特殊的Async Command實現,它看起來相當複雜。

這就是說,它暴露了第二個CancelCommand。你不包括XAML中的取消按鈕,但結合應該是這個樣子:

Command="{Binding ComandoProcessarArquivo.CancelCommand}" 

如果這個命令執行的東西,你發現(而不是一個企業標準),那麼我會建議其他的選擇四處尋找。 Nito.AsyncEx有一個很好的現代異步命令實現。

+0

Andrew,我正在開發公司的軟件,他們使用這個課程,但我可以自由改變。我已經嘗試過,像你說的那樣執行cancelcommand,但它沒有工作:(我會盡快查看nito.asyncex,你有任何鏈接?謝謝你隊友:P –