我将在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是一种很好的做法。
所以,我正在寻找一种在OnBackKeyPress
方法中使用WPtoolkit或Coding4Fun窗口的方法,或者在Silverlight应用程序中使用XNA的任何解释(任何推荐或关于批准这类应用程序的商店信息)。
答案 0 :(得分: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"