---
# System prepended metadata

title: WinUI 3 メッセージボックスを表示する 2022-07-29
tags: [WinUI 3, メッセージボックス, MessageBox]

---

---
lang: ja-jp
breaks: true
---
# WinUI 3 メッセージボックスを表示する 2022-07-29

## 実行結果

![](https://i.imgur.com/XiBZ1Ka.png)

![](https://i.imgur.com/nfeL7mP.png)

## MainWindow.xaml

```xaml=
<Window
	x:Class="App25.MainWindow"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:local="using:App25"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	mc:Ignorable="d"
	xmlns:viewModels="clr-namespace:App25.viewModels"
	>

    <Grid
        >
        <StackPanel 
			Orientation="Vertical" 
			HorizontalAlignment="Center" 
			VerticalAlignment="Center"
			>

            <ToggleSwitch 
				AutomationProperties.Name="simple ToggleSwitch" 
				x:Name="dark_switch" 
				CornerRadius="3" 
				VerticalAlignment="Center"  
				HorizontalAlignment="Right" 
				MinWidth="0" 
				HorizontalContentAlignment="Center" 
				VerticalContentAlignment="Center" 
				IsOn="{x:Bind ViewModel.Dark_switch, Mode=TwoWay}"
				/>
            <Button x:Name="UpdatingThePackagesList" Height="50" Click="UpdatingThePackagesList_ClickAsync">Updating the packages list</Button>

        </StackPanel>
    </Grid>
    
</Window>
```

## MainWindow.xaml.cs

```csharp=
public sealed partial class MainWindow : Window
{
    private MainWindowViewModel ViewModel { get; init; }

    public MainWindow()
    {
        this.InitializeComponent();

        ViewModel = new MainWindowViewModel();
        ViewModel.PropertyChanged_Dark_switch += ViewModel_PropertyChanged_Dark_switch;
    }
    
    internal async Task ShowMessageBox(string content)
    {
        ContentDialog contentDialog = new ContentDialog
        {
            Title = "message",
            Content = content,
            CloseButtonText = "OK",
        };

        contentDialog.XamlRoot = this.Content.XamlRoot;

        if (contentDialog is FrameworkElement frameworkElement)
        {
            ElementTheme elementTheme = GetElementTheme();
            frameworkElement.RequestedTheme = elementTheme;
        }

        ContentDialogResult result = await contentDialog.ShowAsync();
    }

    internal ElementTheme GetElementTheme()
    {
        ElementTheme elementTheme = ElementTheme.Default;

        ToggleSwitch toggleSwitch = this.dark_switch;
        if (toggleSwitch != null)
        {
            if (toggleSwitch.IsOn)
            {
                elementTheme = ElementTheme.Dark;
            }
            else
            {
                elementTheme = ElementTheme.Light;
            }
        }

        return elementTheme;
    }

    private void ViewModel_PropertyChanged_Dark_switch(object sender, PropertyChangedEventArgs e)
    {
        if (base.Content is FrameworkElement frameworkElement)
        {
            ElementTheme elementTheme = GetElementTheme();
            frameworkElement.RequestedTheme = elementTheme;
        }
    }
    
    private async void UpdatingThePackagesList_ClickAsync(object sender, RoutedEventArgs e)
    {
        await ViewModel.UpdateNugetPackagesListAsync();
    }
}
```

## MainWindowViewModel.cs

```csharp=
namespace App25.viewModels;

internal sealed class MainWindowViewModel:
    INotifyPropertyChanged
{
    MainWindow m_window;

    public MainWindowViewModel(
        MainWindow window
        )
        : base()
    {
        m_window = window;
    }

    public async Task UpdateNugetPackagesListAsync()
    {
        var msg = "Select the packageSourceKey.";
        await m_window.ShowMessageBox(msg);
        return;
    }

    private bool m_dark_switch;
    public bool Dark_switch { get { return this.m_dark_switch; } set { if (value == this.m_dark_switch) { return; } this.m_dark_switch = value; OnPropertyChanged(nameof(Dark_switch)); } }

    public event PropertyChangedEventHandler PropertyChanged;
    public event PropertyChangedEventHandler PropertyChanged_Dark_switch;
    private void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        switch (propertyName)
        {
            case nameof(Dark_switch):
                PropertyChanged_Dark_switch?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                break;
        }
    }
}
```

###### tags: `WinUI 3` `メッセージボックス` `MessageBox`
