win8开发笔记6 消息框
直接上代码:
1. 界面上添加三个按钮和一个提示框:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <StackPanel VerticalAlignment="Center" Margin="34,558,-34,137"> <StackPanel Orientation="Horizontal"> <Button Content="一个按钮的消息框" Click="onOKClick"/> <Button Content="两个按钮的消息框" Click="onYesNoClick"/> <Button Content="三个按钮的消息框" Click="onThirdClick"/> </StackPanel> <TextBlock x:Name="tbTip" Margin="3,6,0,0" FontSize="24"/> </StackPanel> </Grid>
2. 相关函数如下:
private async void onOKClick(object sender, RoutedEventArgs e) { MessageDialog msg = new MessageDialog("只带一个按钮的消息框。", "提示"); msg.Commands.Add(new UICommand("确定", new UICommandInvokedHandler(OnUICommand))); await msg.ShowAsync(); } private async void onYesNoClick(object sender, RoutedEventArgs e) { MessageDialog msg = new MessageDialog("带两个按钮的消息框。"); msg.Commands.Add(new UICommand("是", new UICommandInvokedHandler(OnUICommand))); msg.Commands.Add(new UICommand("否", new UICommandInvokedHandler(OnUICommand))); await msg.ShowAsync(); } private async void onThirdClick(object sender, RoutedEventArgs e) { MessageDialog msg = new MessageDialog("带三个按钮的消息框。"); msg.Commands.Add(new UICommand("重试", new UICommandInvokedHandler(OnUICommand))); msg.Commands.Add(new UICommand("忽略", new UICommandInvokedHandler(OnUICommand))); msg.Commands.Add(new UICommand("取消", new UICommandInvokedHandler(OnUICommand))); // 默认按钮索引 msg.CancelCommandIndex = 2; msg.DefaultCommandIndex = 0; await msg.ShowAsync(); } async void OnUICommand(IUICommand cmd) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() => { this.tbTip.Text = "您点击了 " + cmd.Label + " 按钮。"; }); }