工作与Windows Phone 8.1返回键按事件
本文关键字:返回 事件 Windows Phone 工作 | 更新日期: 2023-09-27 18:12:13
我正在我的Windows Phone 8.1应用程序上工作。我想在用户按下后退键时显示消息。我知道密码,但有点不对劲。Visual Studio显示MessageBox, MessageBoxResult下面的红线
如何在Windows Phone 8.1中显示消息?我认为这在WP7操作系统之后有所改变。
下面是我的代码示例。
public MainPage()
{
this.InitializeComponent();
progRing.IsActive = true;
Window.Current.SizeChanged += Current_SizeChanged;
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
this.NavigationCacheMode = NavigationCacheMode.Required;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
}
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);
}
根据Windows.Phone.UI.Input命名空间判断,您的目标是基于WinRT XAML的应用程序,而不是Silverlight WP8.1在WinRT中没有MessageBox.Show()方法你必须使用MessageDialog类优点是你可以自定义对话框上按钮的名称,而且这个过程现在是异步的,这意味着当消息对话框显示时,它不会阻止你的应用程序运行
using Windows.UI.Popups;
...
MessageDialog dialogbox= new MessageDialog("Your message content", "title");
await dialogbox.ShowAsync();
JayDev的答案是完整正确的
JayDev回答的一个问题是WinRT中没有"override onBackKeyPress"。这是你必须做的:
using Windows.Phone.UI.Input
...
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//This should be written here rather than the contructor
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
//This is where all your 'override backkey' code goes
//You can put message dialog and/or cancel the back key using e.Handled = true;
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
//remove the handler before you leave!
HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
}
您需要使用MessageDialog而不是MessageBox。即
MessageDialog message = new MessageDialog("my message");
message.ShowAsync();
MessageDialog在Windows.UI.Popups命名空间中。
具体到您可以使用的场景。
protected async 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?";
MessageDialog msgDialog = new MessageDialog(message, caption);
//OK Button
UICommand okBtn = new UICommand("OK");
msgDialog.Commands.Add(okBtn);
//Cancel Button
UICommand cancelBtn = new UICommand("Cancel");
cancelBtn.Invoked = (s) => { e.Cancel = true; };
msgDialog.Commands.Add(cancelBtn);
//Show message
await msgDialog.ShowAsync();
base.OnBackKeyPress(e);
}
试试这个:
MessageBoxResult res= MessageBox.Show(message,caption, MessageBoxButton.OKCancel);
if (res == MessageBoxResult.OK)
{
//Do Action
}
else
{
//Do Action
}