Pergunta

I am going to implement custom confirmation dialog in OnBackKeyPress method. It is easy to do with native message box:

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

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

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

It works, but I dislike limitation with two buttons so I am looking for something else.

I have checked 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();
  }
}

And 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();
}

Both looks good, but don't work in OnBackKeyPress method (show and immediately disappear without any my action).

Moreover, I've tried 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);
  }
}

It works as I expected (has customs in OnBackKeyPress method), but I don't sure that using XNA inside Silverlight app is good practice.

So, I am looking a way to use WPtoolkit or Coding4Fun windows inside OnBackKeyPress method or any explanation about using XNA inside Silverlight app (any recomendation or info about approval such kind app by store).

Foi útil?

Solução

Just use the dispatcher to delay the messagebox until you get out of the OnBackKeyPress event, and it should work:

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

Alternatively, you can use the BackKeyPress event instead of overriding the OnBackKeyPress method. This way, you don't need to use the dispatcher:

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();
    }
}

And in the XAML:

BackKeyPress="Page_BackKeyPress"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top