zoukankan      html  css  js  c++  java
  • 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..欢迎大家留言讨论。

    MessageBoxDemo.rar

  • 相关阅读:
    Linux实时性分析-schedule-调度器
    中断解析
    网络商城-PrestaShop
    和学生的学习互动记录(10嵌)
    QQ记录
    Windows7硬盘安装Fedora16图文教程
    今目标登录时报网络错误E110
    vs环境配置——vs快捷键配置——vs插件配置——vs环境设置
    如何防止app接口被别人调用
    mvc4 找到多个与名为“xx”的控制器匹配的类型
  • 原文地址:https://www.cnblogs.com/cracker/p/Windows8_MessageBox_Winrt_metro.html
Copyright © 2011-2022 走看看