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"