使用Windows Phone 8.1的后退键按下事件

5

我正在开发一个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);
    }

2
那些红线是什么意思?也许你的目标是WP8.1 RT,这个问题会有所帮助 - Romasz
3个回答

18
根据 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;
        }

1
我目前还没有50个声望,所以无法回复JayDev的答案。请有人告诉他 :D - Saurabh3321
它像冠军一样工作!非常感谢。我爱你。 :D - ArdaZeytin
请你把删除事件的部分标红。我在事件循环中浪费了很多时间。 - Juan

2
您需要使用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);
}

错误1:'teknoseyir.MainPage.OnBackKeyPress(System.ComponentModel.CancelEventArgs)'是密封类'teknoseyir.MainPage'中的新虚成员 C:\Users\Arda\documents\visual studio 2013\Projects\TeknoSeyir\teknoseyir\teknoseyir.WindowsPhone\MainPage.xaml.cs 58 39 teknoseyir.WindowsPhone 错误2:'teknoseyir.MainPage.OnBackKeyPress(System.ComponentModel.CancelEventArgs)':未找到适当的方法来重写 C:\Users\Arda\documents\visual studio 2013\Projects\TeknoSeyir\teknoseyir\teknoseyir.WindowsPhone\MainPage.xaml.cs 58 39 teknoseyir.WindowsPhone - ArdaZeytin

-2

试试这个:

    MessageBoxResult res= MessageBox.Show(message,caption, MessageBoxButton.OKCancel);
    if (res == MessageBoxResult.OK)
    {
        //Do Action
    }
    else
    {
        //Do Action
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接