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

Windows App SDK, WinUI3, Desktopでダイアログボックスを表示する

$
0
0
こいつを表示するのが今回の目標です。これまでのWPF/Windows Forms/Win32 APIではMessageBoxを使っていました。しかしWinUI3 Desktopでは基本的にこれらは使えません(使えなくはありませんが、WinUI3である意味が…)。 WinUI3 DesktopではUWPと同じようにMessageDialogまたはContentDialogを使います。 public sealed partial class MainWindow : Window { public MainWindow() { this.InitializeComponent(); Handle = this; } public static MainWindow Handle { get; private set; } } MainWindowのインスタンスをゲットできるようにしておいて…。 using Microsoft.UI.Xaml.Controls; using MvvmGen; [ViewModel] public partial class SamplePage1ViewModel { [Command] private async void SampleButton() { var cd = new ContentDialog { Title = "Sample Application", Content = "Hello World!", CloseButtonText = "OK" }; cd.XamlRoot = MainWindow.Handle.Content.XamlRoot; await cd.ShowAsync(); } } ContentDialogを生成してダイアログを表示します。 XamlRootにMainWindowのXamlRootを指定しているところが肝になります。これがないとSystem.Runtime.InteropServices.COMExceptionの例外が発生してしまいます。 <Button Content="Fire!" Command="{Binding SampleButtonCommand}"/> そしてもちろん、XAMLでボタンを設置して、SampleButtonにbindします。 コマンドの実装にはMvvmGenを使用しています。MvvmGenを使わなくてもダイアログの表示はできます。[ViewModel]属性、[Command]属性はついての詳細は以下の記事を参照してください。

Viewing all articles
Browse latest Browse all 9707

Trending Articles