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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM