c# - 使用 Windows Phone 8.1 后退键按下事件

标签 c# windows-phone-8 messagebox

我正在开发我的 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/

相关文章:

c# - 将日期时间选择器设为灰色?

c# - 在linq的where方法中使用多个条件

c++ - Direct3D 中的 Windows Phone 8 触摸位置

c# - 从 Tap 事件获取绑定(bind)对象

c# - 为什么 AutoMapper 嵌套映射在没有内部类型的 CreateMap 的情况下工作

C# - 在声明类之外调用事件

c# - 使用 POST 请求向 Jira API 发送 JSON 时出现 System.Net.WebException

javascript - 如何使用 PHP 弹出警告消息框?

c++ - (C++) 用于 Linux 的 MessageBox,就像在 MS Windows 中一样

C# 当消息框处于事件状态时执行操作