原文:快速构建Windows 8风格应用22-MessageDialog
本篇博文主要介绍MessageDialog概述、MessageDialog常用属性和方法、如何构建MessageDialog
MessageDialog概述
MessageDialog指的就是对话框。
对话框的命令栏中最多包含三个命令。如果我们指定任何命令,将会有一个默认命令添加到对话框中,目的是关闭对话框。
对话框弹出后界面中所有元素将在对话框下面显示,并且将会阻塞任何触摸事件直到用户进行响应对话框。
另外对话框应该尽量少用。
注意:Windows 8风格应用中取消了MessageBox对象,取而代之的是MessageDialog对象。
MessageDialog常用属性和方法
MessageDialog类包含两类构造函数:
public MessageDialog(string content);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
content参数表示对话框中显示给用户的信息。
2)MessageDialog(String,String)
public MessageDialog(string content, string title);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
content参数表示对话框中显示给用户的信息,title参数表示对话框中显示的标题信息。
MessageDialog类常用方法:
public IAsyncOperation<IUICommand> ShowAsync();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
异步显示对话框。
MessageDialog类常用属性:
1)CancelCommandIndex属性:获取或设置取消命令的索引,用户按下ESC键后执行取消命令;
2)Commands属性:获取对话框的命令栏中显示的命令;
3)Content属性:获取或设置对话框中显示的主要内容;
4)DefaultCommandIndex属性:获取或设置默认命令的索引,用户按下ENTER键后执行默认命令;
5)Options属性:获取或设置对话框中选项;
6)Title属性:获取或设置对话框中标题;
注意:MessageDialog类并不是非常优化,我们需要考虑它在线程模型中的影响。另外关于Windows 8 Store应用中多线程可参考:在多线程环境中使用 Windows 运行时对象。
如何构建MessageDialog
常见的MessageDialog对象呈现的效果如下:
效果1:默认关闭命令
C#代码中未设置任何关闭对话框的命令。如果我们没有设置关闭命令,系统默认添加一个关闭命令。
private async void MessageDialogShow()
{
var messageDialog = new MessageDialog("默认关闭命令");
await messageDialog.ShowAsync();
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
效果2:添加自定义命令
C#代码中添加两个命令,并指定每个命令执行时执行的哪些操作。同时设置了默认命令索引和取消命令索引。
private async void MessageDialogShow()
{
var messageDialog = new MessageDialog("网络连接无效");
messageDialog.Commands.Add(new UICommand("重试", new UICommandInvokedHandler(this.CommandInvokedHandler)));
messageDialog.Commands.Add(new UICommand("取消", new UICommandInvokedHandler(this.CommandInvokedHandler)));
//设置默认命令索引
messageDialog.DefaultCommandIndex = 0;
//设置取消命令索引
messageDialog.CancelCommandIndex = 1;
await messageDialog.ShowAsync();
}
private void CommandInvokedHandler(IUICommand command)
{
//这里编写命令执行的操作代码。
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
我们在添加命令时也可以使用Lambda表达式。
messageDialog.Commands.Add(new UICommand("重试", (command)=>
{
//执行操作
}));
messageDialog.Commands.Add(new UICommand("取消", (command) =>
{
//执行操作
}));
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
也可以指定自定义命令的索引。
messageDialog.Commands.Add(new UICommand("Don‘t install", null, 0));
messageDialog.Commands.Add(new UICommand("Install updates", null, 1));
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
有关MessageDialog的示例代码下载地址:Message dialog sample。