在WP7和WP8中,MessageBox是跟WinForm中一样常用的对话框,但是有一个显著的缺点,就是WP7/8中默认的MessageBox是阻塞线程的。也许是由于这个原因,WP8.1/Win8中采用了异步的MessageDialog对话框, 其扩展性和可定制性更强。但是在很多情况下需要挂起线程等待用户响应或者是单纯怀念MessageBox的简单方便的使用方式。于是我封装了一下WP8.1中的MessageDialog,使其可以像MessageBox一样简单的使用。

public sealed class MessageBox
{
// 摘要:
// 显示包含指定文本和“确定”按钮的消息框。
//
// 参数:
// messageBoxText:
// 要显示的消息。
//
// 异常:
// System.ArgumentNullException:
// messageBoxText 为 null。
public async static Task<MessageBoxResult> Show(string messageBoxText)
{
if (messageBoxText == null)
throw new ArgumentNullException("messageBoxText is null"); var tcs = new TaskCompletionSource<MessageBoxResult>(); var dialog = new MessageDialog(messageBoxText); dialog.Commands.Add(new UICommand("确定", command =>
{
tcs.SetResult((MessageBoxResult)command.Id);
}, MessageBoxResult.OK)); dialog.DefaultCommandIndex = ;
dialog.CancelCommandIndex = ; await dialog.ShowAsync(); return await tcs.Task;
} //
// 摘要:
// 显示包含指定文本、标题栏标题和响应按钮的消息框。
//
// 参数:
// messageBoxText:
// 要显示的消息。
//
// caption:
// 消息框的标题。
//
// button:
// 一个值,用于指示要显示哪个按钮或哪些按钮。
//
// 返回结果:
// 一个值,用于指示用户对消息的响应。
//
// 异常:
// System.ArgumentNullException:
// messageBoxText 为 null。- 或 -caption 为 null。
//
// System.ArgumentException:
// button 不是有效的 System.Windows.MessageBoxButton 值。
public async static Task<MessageBoxResult> Show(string messageBoxText, string caption, MessageBoxButton button)
{
if (messageBoxText == null)
throw new ArgumentNullException("messageBoxText is null");
if (caption == null)
throw new ArgumentNullException("caption is null");
if (button != MessageBoxButton.OK && button != MessageBoxButton.OKCancel)
throw new ArgumentException("button is null"); var tcs = new TaskCompletionSource<MessageBoxResult>(); var dialog = new MessageDialog(messageBoxText,caption); dialog.Commands.Add(new UICommand("确定", command =>
{
tcs.SetResult((MessageBoxResult)command.Id);
}, MessageBoxResult.OK)); if (button==MessageBoxButton.OKCancel)
{
dialog.Commands.Add(new UICommand("取消", command =>
{
tcs.SetResult((MessageBoxResult)command.Id);
}, MessageBoxResult.Cancel));
} dialog.DefaultCommandIndex = ;
if(button==MessageBoxButton.OKCancel)
dialog.CancelCommandIndex = ;
else
dialog.CancelCommandIndex = ; await dialog.ShowAsync(); return await tcs.Task;
}
} // 摘要:
// 指定显示消息框时要包含的按钮。
public enum MessageBoxButton
{
// 摘要:
// 仅显示“确定”按钮。
OK = ,
//
// 摘要:
// 同时显示“确定”和“取消”按钮。
OKCancel = ,
} // 摘要:
// 表示对消息框的用户响应。
public enum MessageBoxResult
{
// 摘要:
// 当前未使用此值。
None = ,
//
// 摘要:
// 用户单击了“确定”按钮。
OK = ,
//
// 摘要:
// 用户单击了“取消”按钮或按下了 ESC。
Cancel = ,
//
// 摘要:
// 当前未使用此值。
Yes = ,
//
// 摘要:
// 当前未使用此值。
No = ,
}

调用方式:

await MessageBox.Show("demo");

await MessageBox.Show("demo", "liubaicai.com", MessageBoxButton.OKCancel);

MessageBoxResult result = await MessageBox.Show("demo", "liubaicai.com", MessageBoxButton.OKCancel);

收工
http://www.liubaicai.net/?p=298

WP8.1StoreApp(WP8.1RT)---MessageBox与MessageDialog的更多相关文章

  1. WP8.1StoreApp(WP8.1RT)---本地Toast

    WP7/8中的Toast是不能在前台弹出的. WP8.1StoreApp可以利用Win8中的方式: private void Toast(string title,string content) { ...

  2. WP8.1StoreApp(WP8.1RT)---发送邮件和短信

    在WP7/8中,发送短信是利用了EmailComposeTask和SmsComposeTask来实现的. 在WP8.1 Store App中,原来的方式已经失效,采用了新的方法:ChatMessage ...

  3. WP8.1StoreApp(WP8.1RT)---第三方启动

    8.1的协议和wp8是相互通用的 被启动: 相比较wp8而言,基本变化不大,但添加方式更直观了 1:打开Package.appxmanifest 2:切换到"声明"选项卡 3:左侧 ...

  4. WP8.1StoreApp(WP8.1RT)---添加推送功能和获取系统信息

    添加推送通知 1:Package.appxmanifest中的声明添加后台任务的推送通知权限 2:var channel = await PushNotificationChannelManager. ...

  5. WP8.1StoreApp(WP8.1RT)---SystemTray的变化

    原Microsoft.Phone.Shell中的SystemTray,已经改到Windows.UI.ViewManagement中StatusBar了. 只能在代码中设置相关属性. 如: 1 2 3 ...

  6. 【WP8】WP8调用官方API使用LED灯

    在WP7中没有相关的API可以直接使用摄像头的LED等,只能通过录像时打开LED等来使用,在WP8中添加了相关的调用接口,可以方便的使用LED灯,并且支持后台,废话不多说,直接上代码 1.在 WMAp ...

  7. WP8.1 Study4:WP8.1中控件集合应用

    1.AutoSuggestBox的应用 在xaml里代码可如下: <AutoSuggestBox Name="autobox" Header="suggestion ...

  8. Windows Phone 8.1又有什么新花样

    今年微软新任CEO提出了“Mobile First and Cloud First”的发展战略,随着微软Mobile First战略的实行,开发者是时候重视Windows Phone了.你可能不相信, ...

  9. “Win10 UAP 开发系列”之主题模式切换

    微软动作真是快,本来想写WP8.1RT系列,结果刚整理了一点就出Win10 UAP了.不过还好RT到Win10的差别还不算太大.前两天参加了Win10开发极客秀,虽然没获奖,不过在韦恩卑鄙的帮助下顺利 ...

随机推荐

  1. 软件测试——等价类划分(EditText * 3)

    一.程序要求 EditBox 同时允许输入三个1到6个英文字符或数字,点击确定结束. 二.测试分析 编号 第一个输入框 第二个输入框 第三个输入框 输出 1 null null null 三个输入框均 ...

  2. ADO.Net学习总结

    一.讲述6个ADO.NET中的常用对象: Connection对象Command对象DataReader对象DataAdapter对象DataSet对象DataTable对象DataRow对象Data ...

  3. .NET 同步 异步 委托

    1.定义委托: using System; using System.Collections.Generic; using System.IO; using System.Linq; using Sy ...

  4. leetcode400

    public class Solution { public int FindNthDigit(int n) { //StringBuilder sb = new StringBuilder(); / ...

  5. jquery中的data-icon和data-role

    转自:https://blog.csdn.net/Sayesan/article/details/83378524 jquery中的data-icon和data-role    data-role参数 ...

  6. 拦截TextBox 双击消息

    Option Explicit Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLo ...

  7. 链表求和12 · Add Two Numbers

    反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...

  8. php 共享内存学习(shmop函数)

    问题:希望可以在进程间共享变量,为共享数据提供快速访问 解决方案:除了可以使用APC模块,还可以用shmop或System V共享内存 //创建键 //将一个可访问的文件路径名转换为一个可供 shmo ...

  9. ASP.NET文件上传大小限制

    上传限制 我们以为的文件大小限制 我们大家都知道ASP.NET为我们提供了文件上传服务器控件FileUpload,默认情况下可上传的最大文件为4M,如果要改变可上传文件大小限制,那么我们可以在web. ...

  10. max文件属性设置,

    之前一直都没找到 用到的时候就是用net 弄了.哎.还在开发东西都是在9上面, 这次脚本必须在 max8 上面 逼的我找到了他 getFileAttribute <filename_string ...