Silverlight C# - 如何在事件完成之前保持循环进度?

时间:2011-01-25 07:42:37

标签: c# silverlight web-applications silverlight-4.0

我正在尝试逐字循环显示文本框中的文本,以便拼写检查它。我已经将文本框的内容拆分成一个数组,并循环遍历数组中的每个单词并通过拼写检查器运行它。找到拼写错误时,我会弹出一个弹出窗口,里面显示一个列表框,以便您可以选择更正。

我遇到的问题是,它只是循环遍历整个数组,最终只显示需要完成的最后一次修正。

如何暂停循环以使其等待选择然后恢复?

这是循环的代码:

  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

2 个答案:

答案 0 :(得分:1)

如果我没有误会,那么你现在要去:

(1)检查循环中的每个单词

(2)在发现错误时暂停循环并弹出建议窗口

(3)用户选择建议词并恢复循环

如果解决方案是:

,我认为它会更好,更容易

(1)检查第一个单词

(2)退出带有错误标志的检查方法,并将位置存储在变量中,弹出建议窗口

(3)用户选择一个建议词,当用户确认了建议时(例如在建议窗口按OK),再次从存储位置启动CheckWordMethod

(4)直到步骤(2)退出没有错误标志,这意味着现在所有单词都是正确的(但要确保在整个过程中,用户只能通过建议窗口修改单词)

答案 1 :(得分:0)

@The Smartest :你的回答引导我走向正确的方向;实际上最终学习了一个新的数据类型!从未使用过Queue。 (这使得 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数据类型非常直接。当单击拼写按钮时,它将TextBox加载到一个数组中,然后我遍历数组以将项目排入articleWords队列,之后它调用CorrectWord()。 CorrectWord()然后在从articleWords和PopUp出列后加载相关列表。关闭事件它清除ListBox并调用CorrectWord(),这将继续返回PopUp,直到没有更多的单词需要纠正。 :)

相关问题