我有一个pivot控件和一个使用selectedIndex ++的按钮,当selectedIndex通过了最后一个条目时,它会打开一个消息框,询问用户是否要进行测验。
但是在测试期间,如果您将按钮垃圾邮件,则在打开MessageBox时会产生0x8000ffff错误。
如何阻止这种情况发生?是否与ui线程太忙或继续移动枢轴有关?在我尝试导航出页面后,按钮事件仍在运行吗?
这就是使用selectedIndex ++
的代码void gotoNextQuestion()
{
if (quizPivot.SelectedIndex < App.settings.currentTest.Questions.Count() - 1)
{
//xScroll -= scrollAmount;
//moveBackground(xScroll);
if (!stoppedPaging)
{
quizPivot.SelectedIndex++;
}
//App.PlaySoundKey("next");
}
else
{
if (App.settings.testMode == App.TestModes.TrainingRecap)
{
MessageBoxResult result;
if (countAnsweredQuestions() == App.settings.currentTest.Questions.Count())
{
stoppedPaging = true;
result = MessageBox.Show("You have reviewed every training question, would you like to go back to the main menu?", "Training Over", MessageBoxButton.OKCancel);
stoppedPaging = false;
if (result == MessageBoxResult.OK)
{
positionableSpriteRadioButton.IsAnswered -= new Action<bool>(Answers_IsAnsweredCompleted);
spriteRadioButton.IsAnswered -= new Action<bool>(Answers_IsAnsweredCompleted);
App.settings.currentTest = null;
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
return;
}
}
}
else
{
MessageBoxResult result;
if (countAnsweredQuestions() == App.settings.currentTest.Questions.Count())
{
stoppedPaging = true;
result = MessageBox.Show("You have answered all of the questions, are you sure you want to finish?", "Are you sure you want to finish?", MessageBoxButton.OKCancel);
stoppedPaging = false;
}
else
{
checkFinishConditions();
}
}
quizPivot.SelectedIndex = 0;
//App.PlaySoundKey("begin");
}
App.settings.currentTest.currentQuestion = quizPivot.SelectedIndex;
}
答案 0 :(得分:2)
嗯,有一件事是肯定的
positionableSpriteRadioButton.IsAnswered -= new Action<bool>(Answers_IsAnsweredCompleted);
那不行。你每次都在创建一个新的Action。因此,没有任何内容具有相同的引用ID,因此不会删除任何内容。
相反,您应该删除Action<bool>
并简单地订阅/取消订阅
positionableSpriteRadioButton.IsAnswered -= Answers_IsAnsweredCompleted;
订阅时
positionableSpriteRadioButton.IsAnswered += Answers_IsAnsweredCompleted;
这样你就可以再次删除它。
但是我建议你不要在这种类型的“向导”中使用枢轴。它滥用了控件,并且会给用户带来非常糟糕的体验。
另外,仅仅因为您导航到另一个页面,并不意味着代码停止运行。除非您在调用return
后添加NavigationService.Navigate
语句,否则将执行同一表达式中的所有代码。
此外,通过在NavigationService.Navigate
的调用中包含对Dispatcher.BeginInvoke
的所有来电,始终确保导航位于UI线程上。