我正在开发我的 Windows Phone 8.1 应用程序。我想在用户按下返回键时显示消息。我知道代码,但出了点问题。 Visual Studio 在 MessageBox 和 MessageBoxResult 下显示红线。
如何在 Windows Phone 8.1 中显示消息?我认为它在WP7 OS之后发生了变化。
这里是我的代码示例。
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 中没有“覆盖 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;
}
关于c# - 使用 Windows Phone 8.1 后退键按下事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24653546/