Repository: dotnet/maui
File: src/Controls/src/Core/Application/Application.Windows.cs (C#)

Code before:
				{
					AppTheme.Dark => ElementTheme.Dark,
					AppTheme.Light => ElementTheme.Light,
					_ => ElementTheme.Default
				};
			}
			return ElementTheme.Default;
		}

		void ApplyThemeToWindow(Window? window, bool followSystem, ElementTheme forcedElementTheme)
		{
			var platformWindow = window?.Handler?.PlatformView as UI.Xaml.Window;

			if (platformWindow is null)
			{
				System.Diagnostics.Debug.WriteLine("ApplyThemeToWindow: platformWindow is null. Unable to apply theme to the root element.");
				return;
			}

			platformWindow.DispatcherQueue.TryEnqueue(() =>
			{
				if (platformWindow.Content is not FrameworkElement root)
				{
					return;
				}

				// Setting RequestedTheme on the root element automatically applies the theme to all child controls. 
				root.RequestedTheme = followSystem ? ElementTheme.Default : forcedElementTheme;

				var isDark = followSystem
					? (UI.Xaml.Application.Current.RequestedTheme == ApplicationTheme.Dark)
					: (forcedElementTheme == ElementTheme.Dark);

				SetTitleBarButtonColors(platformWindow, isDark);
			});
		}

		void SetTitleBarButtonColors(UI.Xaml.Window platformWindow, bool isDark)
		{
			// Color references:
			// https://github.com/microsoft/WinUI-Gallery/blob/main/WinUIGallery/Helpers/TitleBarHelper.cs
			// https://github.com/dotnet/maui/blob/main/src/Core/src/Platform/Windows/MauiWinUIWindow.cs#L218
			if (AppWindowTitleBar.IsCustomizationSupported())
			{
				var titleBar = platformWindow.GetAppWindow()?.TitleBar;
				if (titleBar is not null)
				{
					titleBar.ButtonBackgroundColor = UI.Colors.Transparent;
					titleBar.ButtonInactiveBackgroundColor = UI.Colors.Transparent;
					titleBar.ButtonHoverBackgroundColor = isDark ? TitleBarColors.DarkHoverBackground : TitleBarColors.LightHoverBackground;
					titleBar.ButtonPressedBackgroundColor = isDark ? TitleBarColors.DarkPressedBackground : TitleBarColors.LightPressedBackground;

Review comment (suggestion):
Could also check if `(platformWindow.DispatcherQueue is null)`? Not a common case, but could happen if the Window is disposed.

Code after:
				{
					AppTheme.Dark => ElementTheme.Dark,
					AppTheme.Light => ElementTheme.Light,
					_ => ElementTheme.Default
				};
			}
			return ElementTheme.Default;
		}

		void ApplyThemeToWindow(Window? window, bool followSystem, ElementTheme forcedElementTheme)
		{
			var platformWindow = window?.Handler?.PlatformView as UI.Xaml.Window;

			if (platformWindow is null)
			{
				System.Diagnostics.Debug.WriteLine("ApplyThemeToWindow: platformWindow is null. Unable to apply theme to the root element.");
				return;
			}

			if (platformWindow.DispatcherQueue is null)
			{
				System.Diagnostics.Debug.WriteLine("ApplyThemeToWindow: platformWindow.DispatcherQueue is null. Unable to apply theme to the root element.");
				return;
			}

			platformWindow.DispatcherQueue.TryEnqueue(() =>
			{
				if (platformWindow.Content is not FrameworkElement root)
				{
					return;
				}

				// Setting RequestedTheme on the root element automatically applies the theme to all child controls. 
				root.RequestedTheme = followSystem ? ElementTheme.Default : forcedElementTheme;

				var isDark = followSystem
					? (UI.Xaml.Application.Current.RequestedTheme == ApplicationTheme.Dark)
					: (forcedElementTheme == ElementTheme.Dark);

				SetTitleBarButtonColors(platformWindow, isDark);
			});
		}

		void SetTitleBarButtonColors(UI.Xaml.Window platformWindow, bool isDark)
		{
			// Color references:
			// https://github.com/microsoft/WinUI-Gallery/blob/main/WinUIGallery/Helpers/TitleBarHelper.cs
			// https://github.com/dotnet/maui/blob/main/src/Core/src/Platform/Windows/MauiWinUIWindow.cs#L218
			if (AppWindowTitleBar.IsCustomizationSupported())
			{
				var titleBar = platformWindow.GetAppWindow()?.TitleBar;