Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9707

UWPでメッセージボックスを表示したいとき

$
0
0

UWPでMessageBoxを表示したいとき

各ダイアログの表示確認ようにUWPアプリ作りました。
ソースコード

MessageDialog

シンプルなメッセージ表示ダイアログです。

MessageDialogSample.cs
vardialog=newMessageDialog("コンテンツ","タイトル");_=dialog.ShowAsync();

こんな感じでモーダルで表示されます。
image.png
アラートなどユーザーに何かを伝えたいときに便利です。

ContentDialog

  • ボタン1つの場合
ContentDialogSample1.cs
vardialog=newContentDialog(){Title="OK Dialog Here!!",Content="Click Ok Button Dialog",CloseButtonText="Ok"};varresult=awaitdialog.ShowAsync();

こんな感じ
image.png

  • ボタン2つの場合
ContentDialogSample2.cs
vardialog=newContentDialog(){Title="Yes No Dialog Here!!",Content="Click Yes No Button Dialog",PrimaryButtonText="Yes",CloseButtonText="No"};varresult=awaitdialog.ShowAsync();

こんな感じ
image.png

  • ボタン3つの場合
ContentDialogSample3.cs
vardialog=newContentDialog(){Title="3 Button Dialog Here!!",Content="Click 3 Button Dialog",PrimaryButtonText="Allow",SecondaryButtonText="Delete",CloseButtonText="Cancel"};varresult=awaitdialog.ShowAsync();

こんな感じ
image.png

ContentDialogのShowAsync()の戻り値について

namespaceWindows.UI.Xaml.Controls{[ContractVersion(typeof(UniversalApiContract),65536)][WebHostHidden]publicenumContentDialogResult{None=0,Primary=1,Secondary=2}}

ContentDialogのプロパティに設定した値に対応するボタンが押された際、下記の戻り値を返却します。

プロパティコマンド戻り値
CloseButtonTextCloseButtonCommandNone
PrimaryButtonTextPrimaryButtonCommandPrimary
SecondaryButtonTextSecondaryButtonCommandSecondary

また、それぞれコマンドプロパティも実装されているので、コマンドを作成してプロパティに設定することも可能です。

MessageDialogクラスがsealedクラスであるのに対してContentDialogクラスは継承できるのでレイアウトを拡張したり、好きなコントロールを配置可能なので使いやすいと思います。

応用 InputDialog

ContentDailogクラスを拡張して1つのテキストボックスを持つダイアログクラスを作成しました。
image.png


Viewing all articles
Browse latest Browse all 9707

Trending Articles