use*_*145 3 c# windows-phone-7 windows-phone-8
当我从我的应用程序点击时,我需要将窗口后退按钮.我尝试了下面的方法,但它在Windows Phone 8中不起作用?
方法1)
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
var result = MessageBox.Show("Do you want to exit?", "Attention!",
MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
// base.OnBackKeyPress(e);
return;
}
e.Cancel = true;
}
Run Code Online (Sandbox Code Playgroud)
方法2)
a)在xaml标题中写道
BackKeyPress="MyBackKeyPress"
Run Code Online (Sandbox Code Playgroud)
b)在构造函数中初始化它
BackKeyPress += OnBackKeyPress;
Run Code Online (Sandbox Code Playgroud)
c)并调用此方法
private void MyBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true; //tell system you have handled it
//you c
}
Run Code Online (Sandbox Code Playgroud)
这两种方法都不起作用.当我点击后退按钮时,应用程序被破坏,无法控制后门按钮.有帮助吗?
根据认证要求,不建议覆盖后退按钮 -
从应用程序的第一个屏幕按"后退"按钮必须关闭该应用程序.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh184840%28v=vs.105%29.aspx
您可以尝试此代码
protected override void OnBackKeyPress(CancelEventArgs e)
{
if(MessageBox.Show("Are you sure you want to exit?","Confirm Exit?",
MessageBoxButton.OKCancel) != MessageBoxResult.OK)
{
e.Cancel = true;
}
}
Run Code Online (Sandbox Code Playgroud)