在我的Windows 8.1应用程序中,我使用MessageBox.Show()来弹出消息。这在UWP中已经消失了。我该如何显示信息?
答案 0 :(得分:8)
是的,确实是这样的,新方法是使用MessageDialog类。您必须创建该类型的对象。您还可以添加按钮。我认为这有点复杂。但你也可以在这里使用一些快捷方式。要显示消息,请使用:
await new MessageDialog("Your message here", "Title of the message dialog").ShowAsync();
To show an simple Yes/No message, you can do it like this:
MessageDialog dialog = new MessageDialog("Yes or no?");
dialog.Commands.Add(new UICommand("Yes", null));
dialog.Commands.Add(new UICommand("No", null));
dialog.DefaultCommandIndex = 0;
dialog.CancelCommandIndex = 1;
var cmd = await dialog.ShowAsync();
if (cmd.Label == "Yes")
{
// do something
}
答案 1 :(得分:5)
看一下Windows.UI.Popups.MessageDialog
课程,试试这个:
// Create a MessageDialog
var dialog = new MessageDialog("This is my content", "Title");
// If you want to add custom buttons
dialog.Commands.Add(new UICommand("Click me!", delegate (IUICommand command)
{
// Your command action here
}));
// Show dialog and save result
var result = await dialog.ShowAsync();
答案 2 :(得分:0)
最好将MessageDialog代码放入具有关键字 async 的函数中,并返回任务类型,例如:
public async Task displayMessageAsync(String title, String content,String dialogType)
{
var messageDialog = new MessageDialog(content, title);
if (dialogType == "notification")
{
//Do nothing here.Display normal notification MessageDialog
}
else
{
//Dipplay questions-Yes or No- MessageDialog
messageDialog.Commands.Add(new UICommand("Yes", null));
messageDialog.Commands.Add(new UICommand("No", null));
messageDialog.DefaultCommandIndex = 0;
}
messageDialog.CancelCommandIndex = 1;
var cmdResult = await messageDialog.ShowAsync();
if (cmdResult.Label == "Yes")
{
Debug.WriteLine("My Dialog answer label is:: " + cmdResult.Label);
}
else
{
Debug.WriteLine("My Dialog answer label is:: " + cmdResult.Label);
}
}