[arm64][Settings]Handle the case when .NET load crash on System.Management (#19176)

Prevent crashing with "System.Management requires native modules from the .NET Framework to operate." on creating ThemeListener.
This commit is contained in:
Andrey Nekrasov
2022-07-05 15:25:25 +03:00
committed by GitHub
parent 32fc88abd2
commit d4b62d8118

View File

@@ -12,6 +12,7 @@ using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Helpers; using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Telemetry.Events; using Microsoft.PowerToys.Settings.UI.Library.Telemetry.Events;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
using Microsoft.PowerToys.Telemetry; using Microsoft.PowerToys.Telemetry;
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Windows.UI.Popups; using Windows.UI.Popups;
@@ -58,6 +59,8 @@ namespace Microsoft.PowerToys.Settings.UI
public static Action<string> IPCMessageReceivedCallback { get; set; } public static Action<string> IPCMessageReceivedCallback { get; set; }
private static bool loggedImmersiveDarkException;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="App"/> class. /// Initializes a new instance of the <see cref="App"/> class.
/// Initializes the singleton application object. This is the first line of authored code /// Initializes the singleton application object. This is the first line of authored code
@@ -216,29 +219,40 @@ namespace Microsoft.PowerToys.Settings.UI
public static void HandleThemeChange() public static void HandleThemeChange()
{ {
var isDark = IsDarkTheme(); try
if (settingsWindow != null)
{ {
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(settingsWindow); var isDark = IsDarkTheme();
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark); if (settingsWindow != null)
} {
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(settingsWindow);
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
}
if (oobeWindow != null) if (oobeWindow != null)
{ {
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(oobeWindow); var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(oobeWindow);
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark); ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
} }
var selectedTheme = SelectedTheme(); var selectedTheme = SelectedTheme();
if (selectedTheme == "SYSTEM") if (selectedTheme == "SYSTEM")
{ {
themeListener = new ThemeListener(); themeListener = new ThemeListener();
themeListener.ThemeChanged += (_) => HandleThemeChange(); themeListener.ThemeChanged += (_) => HandleThemeChange();
}
else if (themeListener != null)
{
themeListener.Dispose();
themeListener = null;
}
} }
else if (themeListener != null) catch (Exception e)
{ {
themeListener.Dispose(); if (!loggedImmersiveDarkException)
themeListener = null; {
Logger.LogError($"HandleThemeChange exception. Please install .NET 4.", e);
loggedImmersiveDarkException = true;
}
} }
} }