Silverlight C# - 如何保持循环进度,直到一个事件完成?
我试图通过单词循环显示文本框中的文本,以便对其进行拼写检查。我将文本框的内容分割成一个数组,并遍历数组中的每个单词并通过拼写检查程序运行。当找到拼写错误时,我在弹出窗口中显示一个列表框,以便您可以选择更正。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)检查每个单词的循环
(2)暂停循环时找到错误并弹出建议窗口
(3)用户选择一个建议字和恢复循环
我认为这是更好,更容易,如果解决办法是:
(1)检查这个词的第一个
(2)退出与一个错误标志检查方法,并存储在一个位置变量,弹出建议窗口
(3)用户选择建议字,并且当用户已经确认了建议(例如,在建议窗口中按OK),再次从存储的位置开始CheckWordMethod
(4)直到第(2)步退出时没有错误标志,这意味着所有单词现在都是正确的(但要确保在整个过程中,用户只能通过您的建议窗口修改单词)
@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,直到没有更多的单词被纠正。 :)