我正在开发一个简单的游戏,如果用户点击Back Button,我不希望它回到上一页,也不想终止应用程序。我想展示一个MessagePrompt (来自Coding4Fun控件库),询问用户是否真的想返回主菜单(并且在游戏中失去了所有的进展)。
问题是,当我实现下面的代码时,MessagePrompt确实会出现,但很快就会关闭。任何暗示发生了什么。如果在常规按钮中应用完全相同的代码(除了e.Cancel和base.OnKeyPress ),则正确地显示MessagePrompt。我显然是在搞砸这个BackButton。
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
MessagePrompt abortConfirm = new MessagePrompt();
abortConfirm.Title = "Are you sure you want to go to Main Menu?";
abortConfirm.Message = "All progress in the current game will be lost";
Button confirmAbort = new Button();
confirmAbort.Content = "Yes";
confirmAbort.Click += (object sender3, RoutedEventArgs e3) =>
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
};
Button cancelAbort = new Button();
cancelAbort.Content = "No";
cancelAbort.Click += (object sender2, RoutedEventArgs e2) =>
{
abortConfirm.Hide();
};
abortConfirm.ActionPopUpButtons.Clear();
abortConfirm.ActionPopUpButtons.Add(confirmAbort);
abortConfirm.ActionPopUpButtons.Add(cancelAbort);
abortConfirm.Show();
base.OnBackKeyPress(e);
}
有什么线索吗?
Iff我尝试了一个好的旧MessageBox (下面的代码),它的工作原理就像一种魅力。问题是,这个游戏是一个内存游戏,所以如果用户在正确的时间点击后按钮,他将能够部分地看到下面的屏幕(因为MessageBox.Show()暂停了背景上的一切,包括我的DispatcherTimer。而且我已经在使用来自MessagePrompt的Coding4Fun的其他特性,所以一致性也是值得赞赏的。
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
if (MessageBox.Show("Are you sure you want to exit?", "Confirm Exit?",
MessageBoxButton.OKCancel) != MessageBoxResult.OK)
{
e.Cancel = true;
}
else NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
}
发布于 2014-03-06 21:39:21
我不是coding4fun控件方面的专家,但经过测试,这似乎是他们的错,而不是你们的错。
你在这里有一些解决方案:
但请注意,我不确定,只是等待任务将始终给你想要的结果。或者您可以使用等待式await Task.Delay(1);
,但同样地,在这种情况下使用延迟是一种很糟糕的做法。
发布于 2014-03-06 22:11:37
该控件确实对back按钮执行钩子操作,因为back按钮可以关闭提示。
在c4f工具包上创建一个repro,我将仔细研究它。
http://coding4fun.codeplex.com/WorkItem/Create
https://stackoverflow.com/questions/22236002
复制相似问题