国产探花免费观看_亚洲丰满少妇自慰呻吟_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下載。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 元阳县| 天台县| 威海市| 通化市| 峨山| 五莲县| 青浦区| 印江| 通城县| 巴里| 苗栗县| 清原| 三门县| 彭阳县| 广西| 海林市| 万安县| 五指山市| 洞头县| 莱阳市| 平舆县| 睢宁县| 鸡东县| 宜黄县| 营口市| 滕州市| 来凤县| 黑龙江省| 阿图什市| 尚志市| 博白县| 浙江省| 皋兰县| 都匀市| 胶州市| 西贡区| 德格县| 婺源县| 祁东县| 金秀| 外汇|