濮阳杆衣贸易有限公司

主頁 > 知識庫 > Windows 8技巧:Windows 8彈出提示框MessageDialog與await\async關(guān)鍵字應(yīng)用技巧

Windows 8技巧:Windows 8彈出提示框MessageDialog與await\async關(guān)鍵字應(yīng)用技巧

熱門標簽:云客服外呼系統(tǒng)顯示未連接 安徽電話智能外呼系統(tǒng)銷售價格 拉薩防封外呼系統(tǒng)運營商 地產(chǎn)電話機器人價格多少 西安智能電話機器人 洛陽電銷外呼防封系統(tǒng)怎么樣 上網(wǎng)導(dǎo)航地圖標注 南昌外呼電銷機器人 福州語音電銷機器人報價

  在以前Silverlight、WPF中的彈出窗口提示中是MessageBox類中進行顯示的,現(xiàn)在Windows 8中使用Windows.UI.Popups命名空間下的MessageDialog類代替MessageBox。

  MessageDialog類有以下常用方法和屬性:

    ShowAsync():異步彈出消息框.

    Commands:添加命令,在彈出框界面上同步添加相應(yīng)的按鈕.

    DefaultCommandIndex:設(shè)置默認按鈕的索引,按ENTER鍵將激活該索引對應(yīng)的命令按鈕

    CancelCommandIndex:設(shè)置取消退出按鈕的索引,按ESC鍵將激活該索引對應(yīng)的命令按鈕

    Title:彈出消息框的標題

  async:用于方法申明時,此關(guān)鍵字是告訴編譯器在這個方法體內(nèi)可能會有await關(guān)鍵字。

  await:用于異步操作時的模擬同步等待,聲明有此關(guān)鍵字的異步操作需等待異步操作完成之后才繼續(xù)往下運行,但是不會阻塞UI線程。

  注意:使用await關(guān)鍵字的方法體,必須使用async聲明方法

  現(xiàn)在我們通過一個實例來看MessageDialog、async、await:

復(fù)制代碼
代碼如下:

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

復(fù)制代碼
代碼如下:

private async void First_Click(object sender, RoutedEventArgs e)
{
MessageDialog msg = new MessageDialog("Hello World!這是第一個提示.");
msg.Title = "提示1";
var msginfo = await msg.ShowAsync();
}/p> p>

  二:自定義命令集的消息框

復(fù)制代碼
代碼如下:

private async void Secend_Click(object sender, RoutedEventArgs e)
{
MessageDialog msg1 = new MessageDialog("Hello World!這是第二個提示.");
msg1.Title = "提示2";
msg1.Commands.Add(new UICommand("確定", command =>
{
this.tbText.Text = "你點擊了確定按鈕,第二組提示";
}));
msg1.Commands.Add(new UICommand("取消", command =>
{
this.tbText.Text = "你點擊了取消按鈕,第二組提示";
}));
var msg1info = await msg1.ShowAsync();
}

  三:使用await模擬同步方式得到當前使用命令I(lǐng)D運行響應(yīng)的代碼段

復(fù)制代碼
代碼如下:

private async void Third_Click(object sender, RoutedEventArgs e)
{
MessageDialog msg1 = new MessageDialog("Hello World!這是第三個提示.");
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 = "你點擊了確定按鈕,第三組提示";break;
case 1 :
this.tbText.Text = "你點擊了取消按鈕,第三組提示";break;
default:
break;
}
}

  四:將命令方法體單獨出來寫方法體

復(fù)制代碼
代碼如下:

    private async void Fourth_Click(object sender, RoutedEventArgs e)
{
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 = "你點擊了取消按鈕,第四組提示";
}

  最后我們來看運行效果如下圖所示,如需源碼請點擊 Win8Message_jb51net下載。

標簽:朔州 佛山 阿拉善盟 來賓 平頂山 朝陽 長沙 中山

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Windows 8技巧:Windows 8彈出提示框MessageDialog與await\async關(guān)鍵字應(yīng)用技巧》,本文關(guān)鍵詞  Windows,技巧,彈出,提示,框,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Windows 8技巧:Windows 8彈出提示框MessageDialog與await\async關(guān)鍵字應(yīng)用技巧》相關(guān)的同類信息!
  • 本頁收集關(guān)于Windows 8技巧:Windows 8彈出提示框MessageDialog與await\async關(guān)鍵字應(yīng)用技巧的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    吉首市| 南川市| 清远市| 商南县| 英吉沙县| 上犹县| 南通市| 山阴县| 新河县| 溧阳市| 新营市| 阳泉市| 兴海县| 高陵县| 宁化县| 油尖旺区| 元朗区| 石门县| 枣强县| 资兴市| 凉山| 巨野县| 海盐县| 吉林市| 佳木斯市| 容城县| 林西县| 通州区| 扎鲁特旗| 无为县| 巴林左旗| 南投市| 安塞县| 锦屏县| 泾川县| 南木林县| 台前县| 滦南县| 上饶市| 桐柏县| 安岳县|