windows8开发笔记(1)-找回丢失的MessageBox类
众所周知,Windows8已经取消了MessageBox这个类,如果要弹出消息必须用MessageDialog来实现,从Winform到wpf到Silverlight甚至到Wp7一直用了那么多年的MessageBox就这样消失了..取而代之的是更加强大的MessageDialog,虽然MessageDialog很强大..但是用起来并没有MessageBox方便,而绝大多数Windows8 开发者都会拥有WP,Sl,Wpf之类开发经验..在移植方面用起来很不方便,我也有很多不舍....于是今天我来“找回丢失的MessageBox类”。下面是我的实现方法。
首先添加2个枚举分别是MessageBoxResult和MessageBoxButton
// 摘要: // 指定显示消息框时要包含的按钮。 public enum MessageBoxButton { // 摘要: // 仅显示“确定”按钮。 OK = 0, // // 摘要: // 同时显示“确定”和“取消”按钮。 OKCancel = 1, }
// 摘要: // 表示对消息框的用户响应。 public enum MessageBoxResult { // 摘要: // 当前未使用此值。 None = 0, // // 摘要: // 用户单击了“确定”按钮。 OK = 1, // // 摘要: // 用户单击了“取消”按钮或按下了 Esc。 Cancel = 2, // // 摘要: // 当前未使用此值。 Yes = 6, // // 摘要: // 当前未使用此值。 No = 7, }
上面2个枚举是原先MessageBox里面所需要用到的,大家应该很熟悉了..接下来就是对MessageDialog进行一下封装
public class MessageBox { static string okStr = "确定", cancelStr = "取消", captionStr = "提示"; public async static Task<IUICommand> Show(string message) { MessageBox msg = new MessageBox(); return await msg.ShowMessage(message); } public async static Task<MessageBoxResult> Show(string messageBoxText, string caption, MessageBoxButton button) { MessageBox box = new MessageBox(); var result = await box.ShowMessage(messageBoxText, caption, MessageBoxButton.OKCancel); return getResult(result); } public async Task<IUICommand> ShowMessage(string message) { return await ShowMessage(message, captionStr, MessageBoxButton.OK); } public async Task<IUICommand> ShowMessage(string messageBoxText, string caption, MessageBoxButton button) { MessageDialog msg = new MessageDialog(messageBoxText, caption); msg.Commands.Add(new UICommand(okStr, CommandInvokedHandler)); if (button == MessageBoxButton.OKCancel) { msg.Commands.Add(new UICommand(cancelStr, CommandInvokedHandler)); } msg.DefaultCommandIndex = 1; msg.CancelCommandIndex = 1; IUICommand result = await msg.ShowAsync(); return result; } public delegate void CompleteHandler(MessageBoxResult result); public CompleteHandler Complete; private void CommandInvokedHandler(IUICommand command) { if (Complete != null) { Complete(getResult(command)); } } private static MessageBoxResult getResult(IUICommand command) { MessageBoxResult msgresult = MessageBoxResult.Cancel; if (command.Label == okStr) { msgresult = MessageBoxResult.OK; } else if (command.Label == cancelStr) { msgresult = MessageBoxResult.Cancel; } else { msgresult = MessageBoxResult.None; } return msgresult; } }
调用方式很简单..可以调用静态方法和实例方法
// MessageBox box = new MessageBox(); box.Complete += ((rl) => { if (rl == MessageBoxResult.OK) { //do work } }); var result = await box.ShowMessage("只弹出确定");
MessageBox box = new MessageBox(); box.Complete += ((rl) => { if (rl == MessageBoxResult.OK) { //do work } }); var result = await box.ShowMessage("弹出确定和取消", "提示", MessageBoxButton.OKCancel);
上面2种是实例的方式调用.和原先的MessageBox可能有点不同..现在来看看静态的调用
MessageBox.Show("只弹出确定");
if (await MessageBox.Show("弹出确定和取消", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK) { MessageBox.Show("点击了确定"); }
是不是发现已经惊人的一致了...只要在原先的MessageBox前面加入await..这对于大家在移植上有很大的帮助。
最后我放上Demo..欢迎大家留言讨论。
分类:
windows 8 metro
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器