0

我将在OnBackKeyPress方法中实现自定义确认对话框。使用本机消息框很容易做到:

protected override void OnBackKeyPress(CancelEventArgs e)
{
  base.OnBackKeyPress(e);

  MessageBoxResult result = MessageBox.Show("Text");

  if(result==MessageBoxResult.OK)
  {
    e.Cancel = true;
  }
}

它有效,但我不喜欢两个按钮的限制,所以我正在寻找其他东西。

我检查了 WPtoolkit:

private bool m_cansel = false;
protected override void OnBackKeyPress(CancelEventArgs e)
{
  base.OnBackKeyPress(e);

  if (!m_cansel)
  {
    e.Cancel = true;
    m_cansel = true;
    var messageBox = new CustomMessageBox
      {
        Title = "Title",
        Message = "Message",
        RightButtonContent = "aas",
        IsLeftButtonEnabled = false,
      };
    messageBox.Dismissed += (sender, args) =>
      {
      };
    messageBox.Show();
  }
}

和 Coding4Fun:

private bool m_cansel = false;
protected override void OnBackKeyPress(CancelEventArgs e)
{
  base.OnBackKeyPress(e);

  if (!m_cansel)
  {
    e.Cancel = true;
    m_cansel = true;
    var messageBox = new MessagePrompt 
      {
        Title = "Title",
        Message = "Message",
      };
    messageBox.Completed += (sender, args) =>
      {
        //throw new NotImplementedException();
      };
    messageBox.Show();
}

两者看起来都不错,但不能按OnBackKeyPress方法工作(显示并立即消失,无需我采取任何行动)。

此外,我尝试过 XNA:

private bool m_cansel = false;
protected override void OnBackKeyPress(CancelEventArgs e)
{
  base.OnBackKeyPress(e);

  if (!m_cansel)
  {
    e.Cancel = true;
    m_cansel = true;
    Guide.BeginShowMessageBox("Version of Windows", "Pick a version of Windows.",
                              new List<string> {"Vista", "Seven"}, 0, MessageBoxIcon.Error,
                              asyncResult =>
                                {
                                  int? returned = Guide.EndShowMessageBox(asyncResult);
                                }, null);
  }
}

它按我的预期工作(在OnBackKeyPress方法上有习惯),但我不确定在 Silverlight 应用程序中使用 XNA 是一种好习惯。

因此,我正在寻找一种在方法内使用 WPtoolkit 或 Coding4Fun 窗口的OnBackKeyPress方法,或任何关于在 Silverlight 应用程序中使用 XNA 的解释(任何关于通过商店批准此类应用程序的建议或信息)。

4

1 回答 1

2

只需使用调度程序延迟消息框,直到您退出 OnBackKeyPress 事件,它应该可以工作:

private bool m_cansel = false;
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    base.OnBackKeyPress(e);

    if (!m_cansel)
    {
        e.Cancel = true;
        m_cansel = true;
        var messageBox = new CustomMessageBox
            {
                Title = "Title",
                Message = "Message",
                RightButtonContent = "aas",
                IsLeftButtonEnabled = false,
            };
        messageBox.Dismissed += (sender, args) =>
            {
            };

        Dispatcher.BeginInvoke(messageBox.Show);
    }
}

或者,您可以使用 BackKeyPress 事件而不是覆盖 OnBackKeyPress 方法。这样,您就不需要使用调度程序:

privatevoid Page_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (!m_cansel)
    {
        e.Cancel = true;
        m_cansel = true;
        var messageBox = new CustomMessageBox
        {
            Title = "Title",
            Message = "Message",
            RightButtonContent = "aas",
            IsLeftButtonEnabled = false,
        };
        messageBox.Dismissed += (s, args) =>
        {
        };

        messageBox.Show();
    }
}

在 XAML 中:

BackKeyPress="Page_BackKeyPress"
于 2013-09-14T10:49:26.890 回答