在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

最新文章

  1. .NET正则表达式匹配Silverlight
  2. 作业一_随笔3_调研Android的开发环境的发展演变
  3. Java虚拟机(JVM)中的内存设置详解
  4. UI实时预览最佳实践(转)
  5. C# List 转Datatable
  6. MYSQL 索引类型、什么情况下用不上索引、什么情况下不推荐使用索引
  7. Spark技术内幕:Worker源码与架构解析
  8. 深入理解PHP的运行模式
  9. java操作elasticsearch实现组合桶聚合
  10. python-校验密码小练习
  11. &lt;数据结构与算法分析&gt;读书笔记--要分析的问题
  12. 20165336 2017-2018-2《Java程序设计》第6周学习总结
  13. Kafka.net使用编程入门(三)
  14. 成为Linux内核高手的四个方法
  15. django之创建第7-5-第二种传值方式(time/1232/xiaodneg)
  16. 通过 CeSi + Supervisor 可视化集中管理服务器节点进程
  17. php-fpm.conf 重要参数 max_children 和 request_terminate_timeout
  18. 【译】第二篇 Replication:分发服务器的作用
  19. 23TCP通信
  20. 数据分析之scipy常用方法(五)

热门文章

  1. Seetaface 向树莓派 移植
  2. lombok 的使用
  3. C#使用UUID生成ID
  4. 禅道 bug指向为数字问题解决过程
  5. jquery ui的css设计二
  6. svg make a face
  7. Lamda所有的Capture均是引用
  8. 克拉 &amp; 24K
  9. 聊一下Python的线程 &amp; GIL
  10. Python_03-数据类型