【问题标题】:Working with Windows Phone 8.1 Back Key Press Event使用 Windows Phone 8.1 返回键按下事件
【发布时间】:2014-07-09 12:20:15
【问题描述】:

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

【问题讨论】:

标签: c# windows-phone-8 messagebox


【解决方案1】:

从 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;
        }

【讨论】:

  • 我还没有 50 名声望,所以无法回复 JayDev 的回答。有人请告诉他:D
  • 它的工作就像一个冠军!很感谢。我爱你。:D
  • 请您将删除事件的部分标为红色。我在事件的循环中浪费了很多时间。谢谢
【解决方案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\文档\visual studio 2013\Projects\TeknoSeyir\teknoseyir\teknoseyir.WindowsPhone\MainPage.xaml.cs 58 39 teknoseyir.WindowsPhone
【解决方案3】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多