原文:快速构建Windows 8风格应用22-MessageDialog

本篇博文主要介绍MessageDialog概述、MessageDialog常用属性和方法、如何构建MessageDialog

 

MessageDialog概述

MessageDialog指的就是对话框。

对话框的命令栏中最多包含三个命令。如果我们指定任何命令,将会有一个默认命令添加到对话框中,目的是关闭对话框。

对话框弹出后界面中所有元素将在对话框下面显示,并且将会阻塞任何触摸事件直到用户进行响应对话框。

另外对话框应该尽量少用。

注意:Windows 8风格应用中取消了MessageBox对象,取而代之的是MessageDialog对象。

 

MessageDialog常用属性和方法

MessageDialog类包含两类构造函数:

1)MessageDialog(String)

 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类常用方法:

1)ShowAsync()

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

快速构建Windows 8风格应用22-MessageDialog的更多相关文章

  1. 快速构建Windows 8风格应用32-构建辅助磁贴

    原文:快速构建Windows 8风格应用32-构建辅助磁贴 引言 Windows Phone中,我们开发者可能会开发的一个功能点是将数据列表中某一项"Pin To Start(固定到开始屏幕 ...

  2. 快速构建Windows 8风格应用17-布局控件

    原文:快速构建Windows 8风格应用17-布局控件 本篇博文主要介绍三种常用的布局控件:Canvas.Grid.StackPanel. Panel类是开发Windows 8 Store应用中一个重 ...

  3. 快速构建Windows 8风格应用15-ShareContract构建

    原文:快速构建Windows 8风格应用15-ShareContract构建 本篇博文主要介绍共享数据包.如何构建共享源.如何构建共享目标.DataTransferManager类. 共享数据包 Da ...

  4. 快速构建Windows 8风格应用14-ShareContract概述及原理

    原文:快速构建Windows 8风格应用14-ShareContract概述及原理 本篇博文主要介绍Share Contract概述.Share Contract实现原理.实现Share Contra ...

  5. 快速构建Windows 8风格应用13-SearchContract构建

    原文:快速构建Windows 8风格应用13-SearchContract构建 本篇博文主要介绍如何在应用中构建SearchContract,相应的原理已经在博文<快速构建Windows 8风格 ...

  6. 快速构建Windows 8风格应用9-竖直视图

    原文:快速构建Windows 8风格应用9-竖直视图 本篇博文主要介绍竖直视图概览.关于竖直视图设计.如何构建竖直视图 竖直视图概览 Windows 8为了支持旋转的设备提供了竖屏视图,我们开发的应用 ...

  7. 快速构建Windows 8风格应用10-设备方向

    原文:快速构建Windows 8风格应用10-设备方向 本篇博文主要介绍常用支持Windows 8操作系统设备的方向.如何获取当前设备方向.DisplayProperties类. 常用支持Window ...

  8. 快速构建Windows 8风格应用11-语义缩放

    原文:快速构建Windows 8风格应用11-语义缩放 本篇博文主要介绍为什么需要语义缩放.什么是语义缩放.如何构建语义缩放. 为什么需要语义缩放 如果用过Windows 8系统的开发者都知道在Win ...

  9. 快速构建Windows 8风格应用12-SearchContract概述及原理

    原文:快速构建Windows 8风格应用12-SearchContract概述及原理 本篇博文主要介绍Search Contract概述.Search Contract面板结构剖析.Search Co ...

随机推荐

  1. UI设计规范

    iphone\ipad.android UI设计规范对比 http://blog.163.com/leenell@yeah/blog/static/95840991201302210451710/ A ...

  2. 3-08. 栈模拟队列(25)(ZJU_PAT 模拟)

    主题链接:http://pat.zju.edu.cn/contests/ds/3-08 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操 ...

  3. 解决win10远程桌面没法关机问题

    win10远程桌面没法关机问题: 解决方法:alt+f4

  4. C++学习笔记36 (模板的细节明确template specialization)和显式实例(template instantiation)

    C++有时模板很可能无法处理某些类型的. 例如: #include <iostream> using namespace std; class man{ private: string n ...

  5. [Elasticsearch] 部分匹配 (一) - 前缀查询

    部分匹配(Partial Matching) 敏锐的读者可能已经发现到眼下为止,介绍的查询都是在整个词条层面进行操作的. 匹配的最小单元必须是一个词条.你仅仅能找到存在于倒排索引(Inverted I ...

  6. Ajax实践之用户是否存在

    关于Ajax在之前的学习中,已经对它的基础知识有了初步的了解.仅仅是欠实践. 那么接下来就让实践来检验一下真理吧! 基础见:http://blog.csdn.net/liu_yujie2011com/ ...

  7. 有愿意共同发展USB固件做?

    有愿意共同发展USB固件做.现在,它使用STC89S52+PDIUSBD12(环教你玩USB开发板)它实现了一个USB键盘,项目地址:https://github.com/artprogramming ...

  8. 解析grant connect, resource to user语句

    今天同事问了一个问题:"创建用户分配的权限是:grant connect,resource to user;,可是建立view的时候失败了,错误是权限不够,后来我给这个用户分配了创建view ...

  9. Eclipse正在使用Ant扑灭Android数据包错误的解决方案 – Perhaps JAVA_HOME does not point to the JDK

    问题描写叙述: 在Eclipse中执行ant批量打包工具出错,日志信息例如以下: D:\Android\android-sdk-windows\tools\ant\build.xml:601: The ...

  10. .net在arraylist用法

    1.什么是ArrayListArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本号,它提供了例如以下一些优点: 动态的添加和降低元素 实现了ICollection和IL ...