2010-09-16 25 views
7

我創建了一個繁忙的指標 - 基本上是一個標誌旋轉的動畫。我已將它添加到登錄窗口並將Visibility屬性綁定到我的viewmodel的BusyIndi​​catorVisibility屬性。如何強制我的忙指標顯示? (WPF)

當我點擊登錄時,我想在登錄發生時顯示微調(它會調用Web服務以確定登錄憑證是否正確)。但是,當我將可見性設置爲可見時,請繼續登錄,直到登錄完成才顯示微調。在Winforms舊式編碼中,我會添加一個Application.DoEvents。我怎樣才能使微調框出現在MVVM應用程序中?

的代碼是:

 private bool Login() 
     { 
      BusyIndicatorVisibility = Visibility.Visible; 
      var result = false; 
      var status = GetConnectionGenerator().Connect(_model); 
      if (status == ConnectionStatus.Successful) 
      { 
       result = true; 
      } 
      else if (status == ConnectionStatus.LoginFailure) 
      { 
       ShowError("Login Failed"); 
       Password = ""; 
      } 
      else 
      { 
       ShowError("Unknown User"); 
      } 
      BusyIndicatorVisibility = Visibility.Collapsed; 
      return result; 
     } 
+1

+1讓我不寒而慄與DoEvents ;-) – Stimul8d 2010-11-22 11:39:51

回答

8

你必須讓你的登錄異步。您可以使用BackgroundWorker來執行此操作。喜歡的東西:

BusyIndicatorVisibility = Visibility.Visible; 
// Disable here also your UI to not allow the user to do things that are not allowed during login-validation 
BackgroundWorker bgWorker = new BackgroundWorker() ; 
bgWorker.DoWork += (s, e) => { 
    e.Result=Login(); // Do the login. As an example, I return the login-validation-result over e.Result. 
}; 
bgWorker.RunWorkerCompleted += (s, e) => { 
    BusyIndicatorVisibility = Visibility.Collapsed; 
    // Enable here the UI 
    // You can get the login-result via the e.Result. Make sure to check also the e.Error for errors that happended during the login-operation 
}; 
bgWorker.RunWorkerAsync(); 

僅限completness:沒有給予UI刷新登錄發生之前的時間的可能性。這是通過調度員完成的。然而,這是一個黑客,國際海事組織永遠不應該使用。但是如果你對此感興趣,你可以在StackOverflow中搜索wpf doevents

+0

垃圾。我希望那不是答案。我已經繼承了一個巨大的應用程序,用戶界面的東西遍佈整個地方,我有一天可以將這個繁忙的指標添加到大約50個屏幕上。有沒有更簡單的方法? – Unhappy 2010-09-16 09:38:28

+0

另一個問題是,我試圖在登錄屏幕上,它打破了測試:( – Unhappy 2010-09-16 09:39:42

+0

也許是這樣的:在登錄() - 方法,打開一個模式窗口(window.ShowDialog)並進行登錄。在登錄完成後關閉窗口,所以應用程序被阻塞,直到登錄完成。窗口本身顯示應用程序正忙。在窗口內,我將使用BackgroundWorker。 – HCL 2010-09-16 09:49:25

0

您的登錄代碼是否在UI線程上運行?這可能會阻止數據綁定更新。

+0

是的不幸。 – Unhappy 2010-09-16 09:38:52