国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Windows > 正文

Windows 8技巧:Windows 8彈出提示框MessageDialog與await/async關鍵字應用技巧

2019-11-28 03:38:28
字體:
來源:轉載
供稿:網友

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

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

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

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

    DefaultCommandIndex:設置默認按鈕的索引,按ENTER鍵將激活該索引對應的命令按鈕

    CancelCommandIndex:設置取消退出按鈕的索引,按ESC鍵將激活該索引對應的命令按鈕

    Title:彈出消息框的標題

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

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

  注意:使用await關鍵字的方法體,必須使用async聲明方法

  現在我們通過一個實例來看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!這是第一個提示.");
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!這是第二個提示.");
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模擬同步方式得到當前使用命令ID運行響應的代碼段

復制代碼
代碼如下:

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;
}
}

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

復制代碼
代碼如下:

    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下載。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玉环县| 芷江| 奉新县| 拉孜县| 南乐县| 新昌县| 抚松县| 绥滨县| 山东省| 通化市| 昭平县| 顺昌县| 新巴尔虎右旗| 澄城县| 怀来县| 普兰县| 信阳市| 教育| 宁波市| 会理县| 湾仔区| 磐安县| 昌乐县| 霍城县| 吉林省| 平顶山市| 海南省| 青川县| 贡嘎县| 琼结县| 金乡县| 邹城市| 大洼县| 乳源| 沽源县| 宁安市| 弋阳县| 富顺县| 军事| 绥阳县| 来宾市|