在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. 由du,df 得出不同结果反应出的问题

    最近遇到了因为某种异常情况导致某目录下日志暴增,在修复异常情况后,发现pm2 不能启动,查看日志发现原因为空间不足. 使用du -sh查看确实为空间不足.在rm -rf 删除之后,仍然不能启动.这时用 ...

  2. http和https(转)

    一.HTTP协议 最近看了一些网络通信方面的书籍,研究了一下 HTTP 和 TCP/IP,有了一些新的收获和理解,在这里做个归纳和总结. (1)什么是HTTP协议 HTTP (HyperText Tr ...

  3. Back to CNBLOG

    突然发现自己很久都没有写过博客了,感觉有点愧对程序员这个称号... 任重道远,要做的东西很多,越来发现,坚持是最难的,例如写博客. 但起码有有个开始,要有个开始去分享自己的经历,去让别人也知道,你是怎 ...

  4. Android 多线程注意事项

    参考:http://blog.csdn.net/x86android/article/details/14161981 http://geeksun.iteye.com/blog/1447708 An ...

  5. 8 MySQL--单表查询

    单表查询: http://www.cnblogs.com/linhaifeng/articles/7267592.html 1.单表查询的语法 2.关键字的执行优先级(重点) 3.简单查询 4.whe ...

  6. javascript常用验证大全

    1. 长度限制 <script> function test() { if(document.a.b.value.length>50) { alert("不能超过50个字符 ...

  7. python's decorator&wrapper

    [python's decorator&wrapper] decorator A function returning another function, usually applied as ...

  8. CTC 的工作原理

    CTC 的工作原理     Fig. 1. How CTC  combine a word (source: https://distill.pub/2017/ctc/) 这篇文章主要解释CTC 的工 ...

  9. 9.Palindrome Number (INT)

    Determine whether an integer is a palindrome. Do this without extra space. class Solution { public: ...

  10. Notebook computer(Ubuntu)

    ==============Mask_RCNN============== source activate flappbird cd /home/luo/Desktop/MyFile/MaskRCNN ...