C# Silverlight C-如何保持循环进度直到事件完成?
我试着在一个文本框中逐字循环文本,以便拼写检查。我已经将文本框的内容拆分为一个数组,循环遍历数组中的每个单词,并通过拼写检查器运行它。当发现拼写错误时,我有一个弹出窗口,其中显示一个列表框,以便您选择更正 我遇到的问题是,它只是在整个数组中循环,最后只显示需要进行的最后一次更正 如何暂停循环,使其等待选择,然后继续 下面是循环的代码:C# Silverlight C-如何保持循环进度直到事件完成?,c#,silverlight,web-applications,silverlight-4.0,C#,Silverlight,Web Applications,Silverlight 4.0,我试着在一个文本框中逐字循环文本,以便拼写检查。我已经将文本框的内容拆分为一个数组,循环遍历数组中的每个单词,并通过拼写检查器运行它。当发现拼写错误时,我有一个弹出窗口,其中显示一个列表框,以便您选择更正 我遇到的问题是,它只是在整个数组中循环,最后只显示需要进行的最后一次更正 如何暂停循环,使其等待选择,然后继续 下面是循环的代码: foreach(string checkedWord in articleWords) { bool success
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用户选择一个建议词并继续循环 我认为如果解决方案是: 检查第一个单词中的单词 2退出带有错误标志的检查方法,并将位置存储在变量中,弹出建议窗口 3用户选择一个建议词,当用户确认该建议时。按建议窗口上的OK,从存储位置再次启动CheckWordMethod 4直到第2步退出且没有错误标志,这意味着现在所有单词都是正确的,但确保在整个过程中,用户只能通过您的建议窗口修改单词。最聪明的是:您的答案引导我走向正确的方向;实际上最终从中学习到了一种新的数据类型!以前从未使用过队列。这比跟踪我所在阵列的位置要简单得多,因为我第一次想到我必须 无论如何,我会接受你的答案,但这是我最后做的代码:实际替换我尚未实现的文本框中的单词
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;
}
}
}
由于队列数据类型,这一切都非常简单。单击拼写按钮时,它会将文本框加载到一个数组中,然后我在数组中循环,将项目排入articleWords队列,然后调用CorrectWord。CorrectWord在从articleWords中退出队列后,在弹出窗口中加载相关列表。Closed事件它清除列表框并调用CorrectWord,CorrectWord将继续返回弹出窗口,直到没有更多要更正的单词为止: