快速入门:设计消息对话框 (HTML)
[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]
本快速入门提供了有关设计和实现消息对话框的指南。 正如添加消息对话框中所描述的那样,消息对话框在应用中以边对边的形式显示,并且会基于对话框的内容,沿水平方向调整大小。你可以在对话框中添加一个标题、消息以及至多三个按钮。
先决条件
步骤
样例代码显示了这样一个消息对话框:通知用户找不到 Internet 连接并且提示用户响应。本快速入门的大部分代码均位于消息对话框示例中。
1. 创建对话框内容。
// Create the message dialog and set its content
var msg = new Windows.UI.Popups.MessageDialog(
"No internet connection has been found.");
2. 添加按钮。
// Add commands and set their command handlers
msg.commands.append(new Windows.UI.Popups.UICommand(
"Try again",
commandInvokedHandler));
msg.commands.append(
new Windows.UI.Popups.UICommand("Close", commandInvokedHandler));
// Set the command that will be invoked by default
msg.defaultCommandIndex = 0;
// Set the command to be invoked when escape is pressed
msg.cancelCommandIndex = 1;
3. 显示对话框。
// Show the message dialog
msg.showAsync();
4. 如果需要,处理多个模式 UI。
有时,你可能想要从某个对话框启动另一个模型 UI,例如从消息对话框打开文件选取器。Windows 不会让你从原始对话框的命令处理程序中启动额外的模型 UI。
与此相反,你应当在异步操作已完成的处理程序中启动辅助 UI。已完成的处理程序会在对话框遭到损坏时运行并提供异步操作的结果,为此你仍可以判断用户从原始 UI 中单击了哪个命令。
var result = await msg.ShowAsync();
if (result.Label == "Buy")
{
await this.YourCustomFLow(result);
}
private async Task YourCustomFlow(IUICommand command)
{
// Your code here.
}
摘要
本快速入门提供了有关设计和实现消息对话框的指南。