C# WinUI3 指定深色/淺色模式

預設沒有指定時就是依照系統設定,自動使用深色/淺色模式。

在 App.xaml 中指定

深色: Dark
淺色: Light
    
<?xml version="1.0" encoding="utf-8"?>
<Application
    x:Class="WinUiAppNotificationTest.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinUiAppNotificationTest"
    RequestedTheme="Light"
    >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                <!-- Other merged dictionaries here -->
            </ResourceDictionary.MergedDictionaries>
            <!-- Other app resources here -->
        </ResourceDictionary>
    </Application.Resources>
</Application>

    

在 App.xaml.cs 中指定

深色: ApplicationTheme.Dark
淺色: ApplicationTheme.Light
    
    public partial class App : Application
    {
        public App()
        {
            this.InitializeComponent();
            
            App.Current.RequestedTheme = ApplicationTheme.Dark;
        }
    }
    



參考資料:
Microsoft.Learn - Color

留言