我将OnBackKeyPress
方法中实现自定义确认对话框。使用本机消息框很容易做到:
protected override void OnBackKeyPress(CancelEventArgs e)
{
base.OnBackKeyPress(e);
MessageBoxResult result = MessageBox.Show("Text");
if(result==MessageBoxResult.OK)
{
e.Cancel = true;
}
}
它有效,但我不喜欢两个按钮的限制,所以我正在寻找其他东西。
我已经检查了WP工具包:
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();
}
}
和编码4乐趣:
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的任何解释(有关商店批准此类应用程序的任何建议或信息)。
只需使用调度程序延迟消息框,直到您退出 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"