如何使用c#在Windows 8应用程序中显示消息框,如在Windows Phone 7中调用MessageBox.Show()?
Ind*_*ore 48
MessageDialog msgDialog = new MessageDialog("Your message", "Your title");
//OK Button
UICommand okBtn = new UICommand("OK");
okBtn.Invoked = OkBtnClick;
msgDialog.Commands.Add(okBtn);
//Cancel Button
UICommand cancelBtn = new UICommand("Cancel");
cancelBtn.Invoked = CancelBtnClick;
msgDialog.Commands.Add(cancelBtn);
//Show message
msgDialog.ShowAsync();
Run Code Online (Sandbox Code Playgroud)
你的回电话
private void CancelBtnClick(IUICommand command)
{
}
private void OkBtnClick(IUICommand command)
{
}
Run Code Online (Sandbox Code Playgroud)
PS您可以按照本教程了解更多信息.
Ant*_*ula 12
我更简单的方法,对于确认类型的消息框:
var dlg = new MessageDialog("Are you sure?");
dlg.Commands.Add(new UICommand("Yes", null, "YES"));
dlg.Commands.Add(new UICommand("No", null, "NO"));
var op = await dlg.ShowAsync();
if ((string)op.Id == "YES")
{
//Do something
}
Run Code Online (Sandbox Code Playgroud)
小智 8
为了更简单的方法,只需显示消息文本和确定按钮.使用Windows.UI.Popups命名空间 创建messagebox()方法应该是的方法
using Windows.UI.Popups;
protected async void messageBox(string msg)
{
var msgDlg = new Windows.UI.Popups.MessageDialog(msg);
msgDlg.DefaultCommandIndex = 1;
await msgDlg.ShowAsync();
}
Run Code Online (Sandbox Code Playgroud)
然后在你的代码中调用此方法
messageBox("Unexpected error held");
Run Code Online (Sandbox Code Playgroud)