I had to add in this to get it to show in the title bar - because I am using a hack to modify the title bar when the theme changes. The base code always picks the light theme for the title bar.
public MainViewModel DataContext { get; set; }
public MainViewModel ViewModel => DataContext;
public MainWindow()
{
this.InitializeComponent();
var content = (Content as FrameworkElement)!;
content.ActualThemeChanged += (s, e) =>
{
ModifyTitlebarTheme();
};
ModifyTitlebarTheme();
DataContext = Ioc.Default.GetRequiredService<MainViewModel>();
ApplicationView.PreferredLaunchViewSize = new Size(1525, 861);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
var windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
var appWindow = AppWindow.GetFromWindowId(windowId);
var size = new Windows.Graphics.SizeInt32();
size.Width = 1150;
size.Height = 1200;
appWindow.Resize(size);
Microsoft.UI.Windowing.DisplayArea displayArea = Microsoft.UI.Windowing.DisplayArea.GetFromWindowId(windowId, Microsoft.UI.Windowing.DisplayAreaFallback.Nearest);
if (displayArea is not null)
{
var CenteredPosition = appWindow.Position;
CenteredPosition.X = ((displayArea.WorkArea.Width - appWindow.Size.Width) / 2);
CenteredPosition.Y = ((displayArea.WorkArea.Height - appWindow.Size.Height) / 2);
appWindow.Move(CenteredPosition);
}
var appSplash = ((App)Application.Current)._splashScreen;
appSplash?.CenterToScreen(hWnd);
appSplash?.HideSplash(2);
appWindow.Title = $"Multi-Site ARC Plus Config Archive Tool";
hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
string? sExe = Environment.ProcessPath;
if (sExe != null)
{
System.Drawing.Icon? ico = System.Drawing.Icon.ExtractAssociatedIcon(sExe);
if (ico != null)
{
SendMessage(hWnd, WM_SETICON, ICON_BIG, ico.Handle);
// Had to add these
SendMessage(hWnd, WM_SETICON, ICON_SMALL, ico.Handle);
SendMessage(hWnd, WM_SETICON, ICON_SMALL2, ico.Handle);
}
}
}
private void ModifyTitlebarTheme()
{
var content = (Content as FrameworkElement)!;
var value = content.ActualTheme;
var titleBar = AppWindow.TitleBar;
if (value == ElementTheme.Light)
{
titleBar.ForegroundColor = Colors.Black;
titleBar.BackgroundColor = Colors.White;
titleBar.InactiveForegroundColor = Colors.Gray;
titleBar.InactiveBackgroundColor = Colors.White;
titleBar.ButtonForegroundColor = Colors.Black;
titleBar.ButtonBackgroundColor = Colors.White;
titleBar.ButtonInactiveForegroundColor = Colors.Gray;
titleBar.ButtonInactiveBackgroundColor = Colors.White;
titleBar.ButtonHoverForegroundColor = Colors.Black;
titleBar.ButtonHoverBackgroundColor = Color.FromArgb(255, 245, 245, 245);
titleBar.ButtonPressedForegroundColor = Colors.Black;
titleBar.ButtonPressedBackgroundColor = Colors.White;
}
else if (value == ElementTheme.Dark)
{
titleBar.ForegroundColor = Colors.White;
titleBar.BackgroundColor = Color.FromArgb(255, 31, 31, 31);
titleBar.InactiveForegroundColor = Colors.Gray;
titleBar.InactiveBackgroundColor = Color.FromArgb(255, 31, 31, 31);
titleBar.ButtonForegroundColor = Colors.White;
titleBar.ButtonBackgroundColor = Color.FromArgb(255, 31, 31, 31);
titleBar.ButtonInactiveForegroundColor = Colors.Gray;
titleBar.ButtonInactiveBackgroundColor = Color.FromArgb(255, 31, 31, 31);
titleBar.ButtonHoverForegroundColor = Colors.White;
titleBar.ButtonHoverBackgroundColor = Color.FromArgb(255, 51, 51, 51);
titleBar.ButtonPressedForegroundColor = Colors.White;
titleBar.ButtonPressedBackgroundColor = Colors.Gray;
}
// Fixed bug where icon background color was not applied
titleBar.IconShowOptions = IconShowOptions.HideIconAndSystemMenu;
titleBar.IconShowOptions = IconShowOptions.ShowIconAndSystemMenu;
}