【问题标题】:Silverlight C# - How to hold loop progress until an event completes?Silverlight C# - 如何保持循环进度直到事件完成?
【发布时间】:2011-06-15 00:45:17
【问题描述】:

我正在尝试逐字遍历文本框中的文本以进行拼写检查。我已将文本框的内容拆分为一个数组,并遍历数组中的每个单词并通过拼写检查器运行它。当发现拼写错误时,我会弹出一个带有列表框的弹出窗口,以便您选择更正。

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

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

这是循环的代码:

  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 事件发生时,让循环恢复。

提前致谢!

-苏塔

【问题讨论】:

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


    【解决方案1】:

    @The Smartest:您的回答将我引向正确的方向;实际上最终从中学习了一种新的数据类型!以前从未使用过队列。 (这使得它成为一个地狱,比必须跟踪我在数组中的位置要简单得多,因为我最初认为我认为我必须...... :)

    无论如何,我会接受你的回答,但这是我最终做的代码:(我尚未实现的文本框中单词的实际替换。)

    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 上加载。 Closed 事件它清除 ListBox 并调用 CorrectWord(),这将继续带回 PopUp,直到没有更多单词需要更正。 :)

    【讨论】:

      【解决方案2】:

      如果我没有误会,目前你打算:

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

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

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

      我认为如果解决方案是:

      (1)从第一个单词开始检查

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-10
        • 1970-01-01
        • 2018-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多