WP8.1StoreApp(WP8.1RT)---MessageBox与MessageDialog
在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的更多相关文章
- WP8.1StoreApp(WP8.1RT)---本地Toast
WP7/8中的Toast是不能在前台弹出的. WP8.1StoreApp可以利用Win8中的方式: private void Toast(string title,string content) { ...
- WP8.1StoreApp(WP8.1RT)---发送邮件和短信
在WP7/8中,发送短信是利用了EmailComposeTask和SmsComposeTask来实现的. 在WP8.1 Store App中,原来的方式已经失效,采用了新的方法:ChatMessage ...
- WP8.1StoreApp(WP8.1RT)---第三方启动
8.1的协议和wp8是相互通用的 被启动: 相比较wp8而言,基本变化不大,但添加方式更直观了 1:打开Package.appxmanifest 2:切换到"声明"选项卡 3:左侧 ...
- WP8.1StoreApp(WP8.1RT)---添加推送功能和获取系统信息
添加推送通知 1:Package.appxmanifest中的声明添加后台任务的推送通知权限 2:var channel = await PushNotificationChannelManager. ...
- WP8.1StoreApp(WP8.1RT)---SystemTray的变化
原Microsoft.Phone.Shell中的SystemTray,已经改到Windows.UI.ViewManagement中StatusBar了. 只能在代码中设置相关属性. 如: 1 2 3 ...
- 【WP8】WP8调用官方API使用LED灯
在WP7中没有相关的API可以直接使用摄像头的LED等,只能通过录像时打开LED等来使用,在WP8中添加了相关的调用接口,可以方便的使用LED灯,并且支持后台,废话不多说,直接上代码 1.在 WMAp ...
- WP8.1 Study4:WP8.1中控件集合应用
1.AutoSuggestBox的应用 在xaml里代码可如下: <AutoSuggestBox Name="autobox" Header="suggestion ...
- Windows Phone 8.1又有什么新花样
今年微软新任CEO提出了“Mobile First and Cloud First”的发展战略,随着微软Mobile First战略的实行,开发者是时候重视Windows Phone了.你可能不相信, ...
- “Win10 UAP 开发系列”之主题模式切换
微软动作真是快,本来想写WP8.1RT系列,结果刚整理了一点就出Win10 UAP了.不过还好RT到Win10的差别还不算太大.前两天参加了Win10开发极客秀,虽然没获奖,不过在韦恩卑鄙的帮助下顺利 ...
随机推荐
- django-给外键关系传值,删除外键关系
反查: 在表关系里 related_name = '反查name',自己不设置,django也会默认设置为class的小写名字+_set , ex: book_set. 一对一关系赋值: class ...
- 运维基础工具tmux介绍及使用
一.tmux是干什么的? 百度百科的解释很到位:tmux是指通过一个终端登录远程主机并运行后,在其中可以开启多个控制台的终端复用软件. 说白了就是复用软件,复用的什么软件呢? 你可以理解成复用shel ...
- ES6系列_10之Symbol在对象中的作用
在ES5中 对象属性名都是字符串,这容易造成属性名的冲突,比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin 模式),新方法的名字就有可能与现有方法产生冲突,于是 ES6 引入 ...
- Rhythmk 一步一步学 JAVA (20) JAVA enum常用方法
JAVA 枚举定义常用方法: 1.static Enum valueOf(Class enum,String name) 返回指定name的枚举类型 2.Static Enum values[] 返回 ...
- Spring Boot日志集成
Spring Boot日志框架 Spring Boot支持Java Util Logging,Log4j2,Lockback作为日志框架,如果你使用starters启动器,Spring Boot将使用 ...
- 一次点击两次触发addEventListener
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Xcode 中设置部分文件ARC支持
ARC是什么 ARC是iOS 5推出的新功能,全称叫 ARC(Automatic Reference Counting).简单地说,就是代码中自动加入了retain/release,原先需要手动添加的 ...
- 【325】python**:selenium
参考:selenium安装方式 参考:Selenium2(Webdriver)+Python处理浏览器弹窗
- 获取select值及判断是否是数字
代码片段 <div class="container-fluid"> <div class="row"> <div class=& ...
- k8s 1.10 关于rbac的坑
apiserver 启动加上--authorization-mode=RBAC 开启rbac 会生成默认role,最高权限位cluster-admin的cluster role 再关闭rbac(不加 ...