【问题标题】:MessageBox in Windows Phone 8.1Windows Phone 8.1 中的消息框
【发布时间】:2014-05-11 16:15:10
【问题描述】:

我想在硬件上按下后退按钮时向用户显示一个 MessageBox,但它根本不起作用。我尝试了这些变体,但我从未看到 MessageBox:

    // VARIATION 1
    IAsyncResult mbResult = Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox("Warning", "Are you sure you want to leave this page?",
    new string[] { "Yes", "No" }, 0, Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.None, null, null);

    mbResult.AsyncWaitHandle.WaitOne();

    int? yesNo = Microsoft.Xna.Framework.GamerServices.Guide.EndShowMessageBox(mbResult);
    if (yesNo.HasValue)
    {
        if (yesNo.Value == 0)
        {
            // Yes pressed
        }
        else
        {
            // No pressed
        }
    }

    // VARIATION 2
    MessageBoxResult mbr = MessageBox.Show("Are you sure you want to leave this page?", "Warning", MessageBoxButton.OKCancel);

    if(mbr == MessageBoxResult.OK)
    {
        // OK pressed
    }
    else
    {
        // Cancel pressed
    }

如果我将e.Cancel = true 写入OnBackKeyPress 事件,则我无法离开页面,因此代码正在执行,但我从未看到消息框:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = true;
}

可能是什么问题,或者我做错了什么?

【问题讨论】:

    标签: c# silverlight windows-phone-8 messagebox windows-phone-8.1


    【解决方案1】:

    我可以看到您的目标是 Windows Phone 8.1 Silverlight,然后回复 this question 仍然是实际的,因为 at MSDN"

    适用于:Windows Phone 8 和 Windows Phone Silverlight 8.1 | Windows Phone 操作系统 7.1

    在 Windows Phone 8 中,如果您在 OnBackKeyPress(CancelEventArgs) 或 BackKeyPress 事件的处理程序中调用 Show,应用程序将退出。

    MSDN 上也给出了解决方案。很快 - 在 Dispatcher 上运行您的 Messeagebox.Show:

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
          MessageBoxResult mbr = MessageBox.Show("Are you sure you want to leave this page?", "Warning", MessageBoxButton.OKCancel);
    
          if(mbr == MessageBoxResult.OK)
          {   OK pressed  } 
          else
          {   Cancel pressed  }
        });
    }
    

    【讨论】:

    • 请注意,这仅适用于 8.1 Silverlight,不适用于 8.1 Windows 运行时
    • @Matteo 请注意,问题是关于 Silverlight。在运行时模式是相似的,只是方法不同。
    • 是的,但问题没有指定 Silverlight,所以有人可能会感到困惑。
    • @Matteo 班级 MessageBox 说 cleary 那是 Silverlight,也使用 XNA - 只有 Silverlight,OnBackKeyPressed 事件...
    • 我知道!!我刚刚说过有人可能会感到困惑!
    【解决方案2】:

    您可能已经解决了您的问题,但从外观上看,您的硬件后退按钮从未触发过,而是默认触发。

    您需要在初始化组件下添加它以触发您的方法。

    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    

    【讨论】:

    • 在接受的答案中,您可以看到提问者使用的是 Windows Phone 8.1 Silverlight,而不是新的 Windows Phone 8.1 运行时。
    • 正确^ 我注意到,在我发布后,几乎立即有人投票赞成我的回答。我的回答是基于新的。 ://
    【解决方案3】:

    试试这段代码会有所帮助

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        MessageBoxResult Exit = MessageBox.Show("Do You Want To Exit?", "Attention !!!", MessageBoxButton.OKCancel);
        if (Exit == MessageBoxResult.OK)
        {
            Application.Current.Terminate(); //Terminates App    
        }
        else if (Exit == MessageBoxResult.Cancel)
        {
            e.Cancel = true;
        }
    }
    

    【讨论】:

    • 您好,您应该使用代码格式化选项来正确格式化您的代码:选择代码并输入Ctrl + K
    猜你喜欢
    • 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
    相关资源
    最近更新 更多