Silverlight Windows Phone 8:如果按Back键,如何在退出应用程序之前显示messagebox?
我有一个应用程序,当用户退出应用程序至少一次时,需要引起用户的注意。所以我得到下面的代码来显示messagebox。我不知道的是,如果用户已阅读该消息,我如何真正退出应用程序?因为BackKey事件似乎总是会出现在calli设置(OnBackKeyPress)中,或者什么是处理显示消息框而不忽略BackKey的好方法?因为若屏幕上出现了另一个弹出窗口,用户按下了后退键,那个么若我试图自己处理后退键,我似乎会遇到一些异常 我的理想情况是,一旦他/她按下messagebox的“我的退出”按钮,应用程序将立即关闭。如果按“取消”,它将返回而不退出。请帮忙。谢谢 我用过的东西。。。但效果不好Silverlight Windows Phone 8:如果按Back键,如何在退出应用程序之前显示messagebox?,silverlight,windows-phone-7,exit,windows-phone-8,back,Silverlight,Windows Phone 7,Exit,Windows Phone 8,Back,我有一个应用程序,当用户退出应用程序至少一次时,需要引起用户的注意。所以我得到下面的代码来显示messagebox。我不知道的是,如果用户已阅读该消息,我如何真正退出应用程序?因为BackKey事件似乎总是会出现在calli设置(OnBackKeyPress)中,或者什么是处理显示消息框而不忽略BackKey的好方法?因为若屏幕上出现了另一个弹出窗口,用户按下了后退键,那个么若我试图自己处理后退键,我似乎会遇到一些异常 我的理想情况是,一旦他/她按下messagebox的“我的退出”按钮,应用程
private void OnBackKeyPressed(object sender, CancelEventArgs e)
{
e.Cancel = true;
CheckBox checkBox = new CheckBox()
{
Content = "Do not ask me again",
Margin = new Thickness(0, 14, 0, -2)
};
TiltEffect.SetIsTiltEnabled(checkBox, true);
CustomMessageBox messageBox = new CustomMessageBox()
{
Caption = "Would you like to stop and exit?",
Message =
"If you want to continue listen music while doing other stuff, please use Home key instead of Back key",
Content = checkBox,
LeftButtonContent = "Exit",
RightButtonContent = "Cancel",
};
messageBox.Dismissed += (s1, e1) =>
{
switch (e1.Result)
{
case CustomMessageBoxResult.LeftButton: //Exit
return;// What to do here??
case CustomMessageBoxResult.RightButton: //Cancel
case CustomMessageBoxResult.None:
break;
default:
break;
}
};
messageBox.Show();
}
}
不幸的是,没有好的方法来实现你所追求的结果。Windows Phone不允许您以编程方式退出应用程序。大多数人在这种情况下使用的解决方案是抛出一个未经处理的异常。见下文:
不幸的是,没有什么好办法可以达到你想要的结果。Windows Phone不允许您以编程方式退出应用程序。大多数人在这种情况下使用的解决方案是抛出一个未经处理的异常。见下文:
对于WP8,您可以调用
Application.Current.Terminate()
以编程方式退出应用程序。对于WP7,这是不可用的,您必须选择Colin给出的选项之一
但是,如果您这样做,您应该小心满足Microsoft应用商店的认证要求,因为如果您的应用程序表现不符合预期,则可能会被拒绝。对于WP8,您可以调用
Application.Current.Terminate()
以编程方式退出应用程序。对于WP7,这是不可用的,您必须选择Colin给出的选项之一
但是,如果您这样做,您应该小心满足Microsoft应用商店的认证要求,因为如果您的应用程序表现不符合预期,则可能会被拒绝。如果您确实需要按钮显示“退出”,则必须使用ColinE或Paul Annetts解决方案。就我个人而言,我会尝试以另一种方式表达信息,并使用如下的普通消息框:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
string caption = "Stop music and exit?";
string message ="If you want to continue listen music while doing other stuff, please use Home key instead of Back key. Do you still want to exit?";
e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption, MessageBoxButton.OKCancel);
base.OnBackKeyPress(e);
}
messageBox.Dismissed += (s1, e1) =>
{
switch (e1.Result)
{
case CustomMessageBoxResult.LeftButton: //Exit
App.Current.Terminate(); //Put this line followed by break
break;
case CustomMessageBoxResult.RightButton: //Cancel
break;
default:
break;
}
};
这将显示一个消息框,但带有“Ok”和“Cancel”按钮,如果用户按下“Ok”,则直接退出
对于“不要再问了”,可以在设置页面上添加设置。如果用户希望再次看到对话框,请在第一次选择“确定”和“取消”时添加第二个对话框。如果您确实需要按钮显示“退出”,则必须使用ColinE或Paul Annetts解决方案。就我个人而言,我会尝试以另一种方式表达信息,并使用如下的普通消息框:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
string caption = "Stop music and exit?";
string message ="If you want to continue listen music while doing other stuff, please use Home key instead of Back key. Do you still want to exit?";
e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption, MessageBoxButton.OKCancel);
base.OnBackKeyPress(e);
}
messageBox.Dismissed += (s1, e1) =>
{
switch (e1.Result)
{
case CustomMessageBoxResult.LeftButton: //Exit
App.Current.Terminate(); //Put this line followed by break
break;
case CustomMessageBoxResult.RightButton: //Cancel
break;
default:
break;
}
};
这将显示一个消息框,但带有“Ok”和“Cancel”按钮,如果用户按下“Ok”,则直接退出
对于“不要再问了”,可以在设置页面上添加设置。如果用户希望再次看到对话框,则在第一次选择“确定”和“取消”时添加第二个对话框。我使用自定义消息框做了同样的事情。 对代码进行如下修改:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
string caption = "Stop music and exit?";
string message ="If you want to continue listen music while doing other stuff, please use Home key instead of Back key. Do you still want to exit?";
e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption, MessageBoxButton.OKCancel);
base.OnBackKeyPress(e);
}
messageBox.Dismissed += (s1, e1) =>
{
switch (e1.Result)
{
case CustomMessageBoxResult.LeftButton: //Exit
App.Current.Terminate(); //Put this line followed by break
break;
case CustomMessageBoxResult.RightButton: //Cancel
break;
default:
break;
}
};
当用户按退出时,您的应用程序将终止,当按关闭时,“左”按钮的默认操作是关闭消息框。
这应该是工作,如果你把线
break;
在正确的地方
希望这有帮助我使用自定义消息框也做了同样的事情。 对代码进行如下修改:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
string caption = "Stop music and exit?";
string message ="If you want to continue listen music while doing other stuff, please use Home key instead of Back key. Do you still want to exit?";
e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption, MessageBoxButton.OKCancel);
base.OnBackKeyPress(e);
}
messageBox.Dismissed += (s1, e1) =>
{
switch (e1.Result)
{
case CustomMessageBoxResult.LeftButton: //Exit
App.Current.Terminate(); //Put this line followed by break
break;
case CustomMessageBoxResult.RightButton: //Cancel
break;
default:
break;
}
};
当用户按退出时,您的应用程序将终止,当按关闭时,“左”按钮的默认操作是关闭消息框。
这应该是工作,如果你把线
break;
在正确的地方
希望这对您有所帮助您只需先取消按下后退键的事件,然后根据在消息框上单击的按钮,您可以采取以下适当的操作:
private void BackKey_Pressed(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
MessageBoxResult messageBoxResult = MessageBox.Show("Text", "Caption", MessageBoxButton.OKCancel);
if (messageBoxResult == MessageBoxResult.OK)
{
Application.Current.Terminate();
}
}
您只需取消先按下back键的事件,然后根据在消息框上单击的按钮,您可以采取以下适当的操作:
private void BackKey_Pressed(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
MessageBoxResult messageBoxResult = MessageBox.Show("Text", "Caption", MessageBoxButton.OKCancel);
if (messageBoxResult == MessageBoxResult.OK)
{
Application.Current.Terminate();
}
}
我只是在做进一步的测试。这里唯一的问题是,如果按下后退按钮,CustomMessageBox打开,如果用户再次按下后退按钮,应用程序将崩溃。我会设法解决的。我只是在做进一步的测试。这里唯一的问题是,如果按下后退按钮,CustomMessageBox打开,如果用户再次按下后退按钮,应用程序将崩溃。我会想办法解决的。