2011-01-25 39 views
2

我試圖通過單詞循環顯示文本框中的文本,以便對其進行拼寫檢查。我將文本框的內容分割成一個數組,並遍歷數組中的每個單詞並通過拼寫檢查程序運行。當找到拼寫錯誤時,我在彈出窗口中顯示一個列表框,以便您可以選擇更正。Silverlight C# - 如何保持循環進度,直到一個事件完成?

我遇到的問題是它只是循環遍歷整個數組,並且最終只顯示需要完成的最後一次校正。

我該如何暫停循環,以便它等待所做的選擇然後恢復?

下面是循環代碼:

foreach(string checkedWord in articleWords) 
     { 
      bool success = _spellChecker.CheckWord(checkedWord); 
      List<string> suggest; 

      if (!success) 
      { 
       suggest = _spellChecker.GetSuggestions(checkedWord); 
       SpellChecklistBox.Items.Clear(); 

       foreach (string s in suggest) 
       { 

        SpellChecklistBox.Items.Add(new ListBoxItem() { Content = s }); 

       } 
       SpellCheckerPopup.IsOpen = true; 
       SpellChecklistBox.Items.Add(new ListBoxItem() { Content = "   ----------------------" }); 
       SpellChecklistBox.Items.Add(new ListBoxItem() { Content = "Ignore" }); 
      } 


     } 

當SpellCheckerPopup顯示,我在上SelectionChange列表框中的事件觸發。

基本上,我需要以某種方式暫停循環,然後當SelectionChange事件做它的事情,讓循環恢復。

提前致謝!

-Sootah

回答

1

如果我沒有誤會,現在你要:

(1)檢查每個單詞的循環

(2)暫停循環時找到錯誤並彈出建議窗口

(3)用戶選擇一個建議字和恢復循環

我認爲這是更好,更容易,如果解決辦法是:

(1)檢查這個詞的第一個

(2)退出與一個錯誤標誌檢查方法,並存儲在一個位置變量,彈出建議窗口

(3)用戶選擇建議字,並且當用戶已經確認了建議(例如,在建議窗口中按OK),再次從存儲的位置開始CheckWordMethod

(4)直到第(2)步退出時沒有錯誤標誌,這意味着所有單詞現在都是正確的(但要確保在整個過程中,用戶只能通過您的建議窗口修改單詞)

0

@TheSmartst:您的回答讓我朝着正確的方向發展;實際上最終學習了一個新的數據類型!以前從未使用過隊列。 (這使它成爲一個簡單得多的HELL比我需要跟蹤陣列中的位置,因爲我第一次想到我以爲我必須...... :)

無論如何,我會接受你的答案,但這是我最終做的代碼:(實際替換文本框中的單詞,我還沒有實現。)

private void btnSpelling_Click(object sender, RoutedEventArgs e) 
    { 

     SpellChecklistBox.Items.Clear(); 

     string[] articleWordsArray = txtArticle.Text.Split(' '); 

     foreach (string word in articleWordsArray) 
     { 
      articleWords.Enqueue(word); 

     } 

     CorrectWord(); 


    } 

private void SpellChecklistBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     SpellCheckerPopup.IsOpen = false; 
    } 

private void SpellCheckerPopup_Closed(object sender, EventArgs e) 
    { 
     CorrectWord(); 
     SpellChecklistBox.Items.Clear(); 
    } 

Queue<string> articleWords = new Queue<string>(); 


    private void CorrectWord() 
    { 
     if (articleWords.Count() > 0) 
     { 
      string checkedWord = articleWords.Dequeue(); 
      bool success = _spellChecker.CheckWord(checkedWord); 
      List<string> suggest; 

      if (!success) 
      { 
       suggest = _spellChecker.GetSuggestions(checkedWord); 


       foreach (string s in suggest) 
       { 
        SpellChecklistBox.Items.Add(new ListBoxItem() { Content = s }); 
       } 

       SpellCheckerPopup.IsOpen = true; 
       SpellChecklistBox.Items.Add(new ListBoxItem() { Content = "   ----------------------" }); 
       SpellChecklistBox.Items.Add(new ListBoxItem() { Content = "Ignore" }); 
       SpellCheckerPopup.IsOpen = true; 
      } 
     } 
    } 

這一切都是非常簡單的Queue數據類型。當拼寫按鈕被點擊時,它將文本框加載到一個數組中,然後循環遍歷數組,將條目排入articleWords隊列,之後它調用CorrectWord()。 CorrectWord()然後在從articleWords出列並在PopUp上加載相關列表。關閉的事件清除ListBox並調用CorrectWord(),它將繼續帶回PopUp,直到沒有更多的單詞被糾正。 :)

相關問題