Metro Style App 消息框MessageDialog总结

好久没写博客了,最近看到现在 Metro style Apps 文章,示例很少,研究了下 感觉不错把心得分享出来。代码参考了MSDN的消息框MessageDialog的例子。结合自己的体会。

Metro Style App中消息对话框可以显示用户帮助信息,消息对话框由MessageDialog实现。

1、自定义消息框。下面是自己定义的消息框,命令是你想要的操作,此处略(用ToDo来表示)。

        private async void CustomCommandButton_Click2(object sender, RoutedEventArgs e)
        {
            //设置内容和标题
            var messageDialog = new MessageDialog("New updates have been found for this program. Would you like to install the new updates?", "Updates available");

            messageDialog.Commands.Add(new UICommand("Don't install", (command) =>
            {
                //ToDo:
            }));

            messageDialog.Commands.Add(new UICommand("Install updates", (command) =>
            {
                //ToDo:
            }));

            //设置默认按钮
            messageDialog.DefaultCommandIndex = 1;

            await messageDialog.ShowAsync();
        }

 2、 什么按钮都没有增加的时候,默认会有一个Close按钮。

        private async void DefaultCloseCommandButton_Click2(object sender, RoutedEventArgs e)
        {
            var messageDialog = new MessageDialog("You've exceeded your trial period.");
            await messageDialog.ShowAsync();

        }

此时弹出的文本框自由内容,没有标题。

3、使用命令ID。

        private async void CompletedCallbackButton_Click(object sender, RoutedEventArgs e)
        {      
            var messageDialog = new MessageDialog("New updates have been found for this program. Would you like to install the new updates?", "Updates available");

            // 增加命令和命令ID
            messageDialog.Commands.Add(new UICommand("Don't install", null, 0));
            messageDialog.Commands.Add(new UICommand("Install updates", null, 1));

            messageDialog.DefaultCommandIndex = 1;

            var commandChosen = await messageDialog.ShowAsync();

            switch (commandChosen.Id.ToString())
            {
                case "0":
                    //Todo:
                  break;
                case "1":
                    //ToDo:
                  break;             
            }          
        }

此处使用命令ID的形式,然后根据用户选择的命令ID分别做不同的操作。

4、当按下Esc事,取消命令去作用

        private async void CancelCommandButton_Click2(object sender, RoutedEventArgs e)
        {
            var messageDialog = new MessageDialog("No internet connection has been found.");

            //两个命令的CallBack函数是一样的,与上面inline的形式不同。
            messageDialog.Commands.Add(new UICommand("Try again", new UICommandInvokedHandler(this.CommandInvokedHandler2)));
            messageDialog.Commands.Add(new UICommand("Close", new UICommandInvokedHandler(this.CommandInvokedHandler2)));

            messageDialog.DefaultCommandIndex = 0;

            // 当按下Esc事,取消命令去作用
            messageDialog.CancelCommandIndex = 1;

            await messageDialog.ShowAsync();
        }

        private void CommandInvokedHandler2(IUICommand command)
        {
            //ToDo:
        }

 直接在键盘上按下Esc退出按钮,直接调用退出命令。此处要注意,两个命令调用同一个函数CommandInvokedHandler2。

MessageDialog的总结就先介绍到这里,以后有新的感悟在增加上去。Bye。

作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!

原文地址:https://www.cnblogs.com/linlf03/p/2625345.html