在以前Silverlight、WPF中的彈出窗口提示中是MessageBox類中進(jìn)行顯示的,現(xiàn)在Windows 8中使用Windows.UI.Popups命名空間下的MessageDialog類代替MessageBox。
MessageDialog類有以下常用方法和屬性:
ShowAsync():異步彈出消息框.
Commands:添加命令,在彈出框界面上同步添加相應(yīng)的按鈕.
DefaultCommandIndex:設(shè)置默認(rèn)按鈕的索引,按ENTER鍵將激活該索引對(duì)應(yīng)的命令按鈕
CancelCommandIndex:設(shè)置取消退出按鈕的索引,按ESC鍵將激活該索引對(duì)應(yīng)的命令按鈕
Title:彈出消息框的標(biāo)題
async:用于方法申明時(shí),此關(guān)鍵字是告訴編譯器在這個(gè)方法體內(nèi)可能會(huì)有await關(guān)鍵字。
await:用于異步操作時(shí)的模擬同步等待,聲明有此關(guān)鍵字的異步操作需等待異步操作完成之后才繼續(xù)往下運(yùn)行,但是不會(huì)阻塞UI線程。
注意:使用await關(guān)鍵字的方法體,必須使用async聲明方法
現(xiàn)在我們通過一個(gè)實(shí)例來看MessageDialog、async、await:
Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
Button Content="First Msg" HorizontalAlignment="Left"
Margin="430,196,0,0" VerticalAlignment="Top"
Height="51" Width="114" Click="First_Click"/>
Button Content="Secend Msg" HorizontalAlignment="Left"
Margin="606,196,0,0" VerticalAlignment="Top"
Height="51" Width="114" Click="Secend_Click"/>
Button Content="Third Msg" HorizontalAlignment="Left"
Margin="788,196,0,0" VerticalAlignment="Top"
Height="51" Width="114" Click="Third_Click"/>
Button Content="Fourth Msg" HorizontalAlignment="Left"
Margin="975,196,0,0" VerticalAlignment="Top"
Height="51" Width="114" Click="Fourth_Click"/>
TextBlock HorizontalAlignment="Left" Name="tbText"
Margin="573,160,0,0" TextWrapping="Wrap"
Text="TextBlock" VerticalAlignment="Top"
Height="31" Width="565" FontSize="16"/>
/Grid>
一:最簡單的MessageDialog
private async void First_Click(object sender, RoutedEventArgs e)
{
MessageDialog msg = new MessageDialog("Hello World!這是第一個(gè)提示.");
msg.Title = "提示1";
var msginfo = await msg.ShowAsync();
}/p>
p>
二:自定義命令集的消息框
private async void Secend_Click(object sender, RoutedEventArgs e)
{
MessageDialog msg1 = new MessageDialog("Hello World!這是第二個(gè)提示.");
msg1.Title = "提示2";
msg1.Commands.Add(new UICommand("確定", command =>
{
this.tbText.Text = "你點(diǎn)擊了確定按鈕,第二組提示";
}));
msg1.Commands.Add(new UICommand("取消", command =>
{
this.tbText.Text = "你點(diǎn)擊了取消按鈕,第二組提示";
}));
var msg1info = await msg1.ShowAsync();
}
三:使用await模擬同步方式得到當(dāng)前使用命令I(lǐng)D運(yùn)行響應(yīng)的代碼段
private async void Third_Click(object sender, RoutedEventArgs e)
{
MessageDialog msg1 = new MessageDialog("Hello World!這是第三個(gè)提示.");
msg1.Title = "提示3";
msg1.Commands.Add(new UICommand("確定", null, 0));
msg1.Commands.Add(new UICommand("取消", null, 1));
msg1.DefaultCommandIndex = 0;
msg1.CancelCommandIndex = 1;
var msg1info = await msg1.ShowAsync();
switch (Convert.ToInt32(msg1info.Id))
{
case 0 :
this.tbText.Text = "你點(diǎn)擊了確定按鈕,第三組提示";break;
case 1 :
this.tbText.Text = "你點(diǎn)擊了取消按鈕,第三組提示";break;
default:
break;
}
}
四:將命令方法體單獨(dú)出來寫方法體
private async void Fourth_Click(object sender, RoutedEventArgs e)
{
MessageDialog msg1 = new MessageDialog("Hello World!這是第四個(gè)提示.");
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 = "你點(diǎn)擊了確定按鈕,第四組提示";
}
private void ShowTextCancel(IUICommand command)
{
this.tbText.Text = "你點(diǎn)擊了取消按鈕,第四組提示";
}
最后我們來看運(yùn)行效果如下圖所示,如需源碼請(qǐng)點(diǎn)擊 Win8Message_jb51net下載。