2011-09-21 56 views
2

我有一個數據透視控制和一個按鈕,它執行selectedIndex ++,當selectedIndex已經通過最後一個條目,它會打開一個消息框詢問用戶是否他們想要測驗。經歷一個樞軸控制真的很快崩潰

但在測試過程中,如果垃圾郵件按鈕,它會在您打開MessageBox時創建一個0x8000ffff錯誤。

如何阻止這種情況發生?這是否與UI線程太忙或繼續移動樞軸有關?我試圖導出頁面之後,按鈕事件是否仍在運行?

這是做將selectedIndex ++

void gotoNextQuestion() 
{ 
    if (quizPivot.SelectedIndex < App.settings.currentTest.Questions.Count() - 1) 
    { 
     //xScroll -= scrollAmount; 
     //moveBackground(xScroll); 

     if (!stoppedPaging) 
     { 
      quizPivot.SelectedIndex++; 
     } 

     //App.PlaySoundKey("next"); 
    } 
    else 
    { 
     if (App.settings.testMode == App.TestModes.TrainingRecap) 
     { 
      MessageBoxResult result; 

      if (countAnsweredQuestions() == App.settings.currentTest.Questions.Count()) 
      { 
       stoppedPaging = true; 
       result = MessageBox.Show("You have reviewed every training question, would you like to go back to the main menu?", "Training Over", MessageBoxButton.OKCancel); 
       stoppedPaging = false; 
       if (result == MessageBoxResult.OK) 
       { 
        positionableSpriteRadioButton.IsAnswered -= new Action<bool>(Answers_IsAnsweredCompleted); 
        spriteRadioButton.IsAnswered -= new Action<bool>(Answers_IsAnsweredCompleted); 
        App.settings.currentTest = null; 
        NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); 
        return; 
       } 
      } 
     } 
     else 
     { 
      MessageBoxResult result; 

      if (countAnsweredQuestions() == App.settings.currentTest.Questions.Count()) 
      { 
       stoppedPaging = true; 
       result = MessageBox.Show("You have answered all of the questions, are you sure you want to finish?", "Are you sure you want to finish?", MessageBoxButton.OKCancel); 
       stoppedPaging = false; 
      } 
      else 
      { 
       checkFinishConditions(); 
      } 
     } 

     quizPivot.SelectedIndex = 0; 
     //App.PlaySoundKey("begin"); 
    } 

    App.settings.currentTest.currentQuestion = quizPivot.SelectedIndex; 
} 
+0

如果您共享執行selectedIndex ++的代碼,我們可能會提出一種使其更加健壯的方法。 –

+0

這是代碼,謝謝。 –

回答

2

好代碼,有一件事是什麼,肯定

positionableSpriteRadioButton.IsAnswered -= new Action<bool>(Answers_IsAnsweredCompleted); 

這是行不通的。你每次都創建一個新的Action。所以沒有什麼會有相同的參考ID,因此什麼都不會被刪除。

相反,你應該刪除Action<bool>和簡單的訂閱/與

positionableSpriteRadioButton.IsAnswered -= Answers_IsAnsweredCompleted; 

退訂當您訂閱

positionableSpriteRadioButton.IsAnswered += Answers_IsAnsweredCompleted; 

這樣,你實際上可以再次將其刪除。

但我建議你不要使用這種類型的「嚮導」的數據透視表。這是濫用控制,並會給一個非常糟糕的用戶體驗。

此外,只是因爲您導航到另一個頁面,這並不意味着代碼停止運行。除非在呼叫NavigationService.Navigate之後添加return語句,否則將執行相同表達式中的所有代碼。

此外,請務必通過在Dispatcher.BeginInvoke的調用中將所有調用包裝爲NavigationService.Navigate,確保導航位於UI線程上。

+0

我正在刪除該操作,並且只有當用戶說好的時候,在這種情況下。我似乎已經停止使用begin調用發生崩潰。 –

+2

根本沒有刪除任何操作。您完全誤解了.NET中引用的工作方式!查看更新後的答案。 –

+1

感謝您的更新,我現在得到這個。 –