wp8.1的对话框MessageDialog,不是messageBox了

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012431412/article/details/38319749
同样可以用于win8.1
界面布局如下:
<Page
    x:Class="BlueTooth.MessageBoxTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:BlueTooth"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button x:Name="ShowDialog" Height="90" Width="150" HorizontalAlignment="Center" Content="点击我试试?!" Click="ShowDialog_Click"/>
        <TextBlock Name="tbText" Grid.Row="1" Height="130" Width="250" FontSize="16"></TextBlock>
    </Grid>
</Page>

接下来是消息框,其被封装在类里:

private async void ShowDialog_Click(object sender, RoutedEventArgs e)
        {
            //消息框实例1
            //MessageDialog msg = new MessageDialog("Hello World! 这是第一个例子");
            //msg.Title = "提示1";
            //var msgInfo = await msg.ShowAsync(); 

            //消息框实例2:自定义命令集的消息框
            //MessageDialog msg = new MessageDialog("Hello World! 这是第二个例子。");
            //msg.Title = "提示2";
            //msg.Commands.Add(new UICommand("确定", command => { this.tbText.Text = "你点击了按钮,第二组提示。"; }));
            //msg.Commands.Add(new UICommand("取消", command => { this.tbText.Text = "你点击了取消按钮,第二组提示。"; }));
            //var maginfo = await msg.ShowAsync();

            //消息框实例3:使用await模拟同步方式得到当前使用命令ID运行响应的代码段
            //MessageDialog msg = new MessageDialog("Hello World!这是第三个提示。");
            //msg.Title = "提示三";
            //msg.Commands.Add(new UICommand("确定", null, 0));
            //msg.Commands.Add(new UICommand("取消", null, 0));
            //msg.DefaultCommandIndex = 0;
            //msg.CancelCommandIndex = 1;
            //var msginfo = await msg.ShowAsync();
            //switch (Convert.ToInt32(msginfo.Id))
            //{
            //    case 0: this.tbText.Text = "你点击了确认按钮,第三组提示"; break;
            //    case 1: this.tbText.Text = "你点击了取消按钮,第三组提示"; break;
            //    default: break;
            //}

            //消息框实例4:将命令方法体单独出来写方法体
            MessageDialog msg1 = new MessageDialog("Hello World!这是第四个提示.");
            msg1.Title = "提示3";
            msg1.Commands.Add(new UICommand("确定", new UICommandInvokedHandler(this.ShowTextEnter)));
            msg1.Commands.Add(new UICommand("取消", new UICommandInvokedHandler(this.ShowTextCancel)));
            msg1.DefaultCommandIndex = 0;
            msg1.CancelCommandIndex = 1;
            var msg1info = await msg1.ShowAsync();
        }
        private void ShowTextEnter(IUICommand command)
        {
            this.tbText.Text = "你点击了确定按钮,第四组提示";
        }
        private void ShowTextCancel(IUICommand command)
        {
            this.tbText.Text = "你点击了取消按钮,第四组提示";
        }
        
    }

总共写了四种四种对话框的编写方式,第一种是默认的,运行后只有一个确定按钮。第二三四个自己为他添加了取消按钮。点击按钮后,将会在界面中显示一些提示信息。

再来详细介绍一点:我们需要在wp8.1编程时using Windows.UI.Popups命名空间。8.1没有了引用。

MessageDialog类表示对话画框,不再是以前的MessageBox。MessageDialog的构造函数之一:new MessageDialog("你要显示的提示信息!");

然后可以用Title属性给这个对话框一个标题,最后,需要异步显示信息:var msg1info = await msg1.ShowAsync();

最后,关于UICommand参见MSDN的介绍。



猜你喜欢

转载自blog.csdn.net/u012431412/article/details/38319749