From 4ad7db06ae820bf786a6ad062a00b648ca77aca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Sun, 5 Apr 2026 20:21:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(ui):=20=E5=A4=84=E7=90=86=E6=A1=8C=E9=9D=A2?= =?UTF-8?q?=E7=BB=84=E5=90=88=E7=A6=81=E7=94=A8=E6=97=B6=E7=9A=84=E4=B8=BB?= =?UTF-8?q?=E9=A2=98=E5=BA=94=E7=94=A8=E5=BC=82=E5=B8=B8=20#2678?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当系统桌面组合功能被禁用时,应用 Mica/Acrylic 主题会抛出 COM 异常(HRESULT: 0x80263001)。现通过异常捕获机制,在检测到此特定错误时回退到纯色背景方案,确保程序在受限环境下仍能正常运行。 - 在 ApplyThemeToWindow 方法中添加异常处理逻辑 - 引入 ApplyFallbackTheme 方法应用纯色后备主题 - 根据主题类型提供对应的后备背景色 --- .../Helpers/Ui/WindowHelper.cs | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/BetterGenshinImpact/Helpers/Ui/WindowHelper.cs b/BetterGenshinImpact/Helpers/Ui/WindowHelper.cs index d7655fd2..0b859e93 100644 --- a/BetterGenshinImpact/Helpers/Ui/WindowHelper.cs +++ b/BetterGenshinImpact/Helpers/Ui/WindowHelper.cs @@ -1,4 +1,6 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; using System.Windows.Media; using BetterGenshinImpact.Core.Config; using BetterGenshinImpact.GameTask; @@ -9,6 +11,8 @@ namespace BetterGenshinImpact.Helpers.Ui; public class WindowHelper { + private const uint DesktopCompositionDisabledHResult = 0x80263001; + public static void TryApplySystemBackdrop(System.Windows.Window window) { var themeType = TaskContext.Instance().Config.CommonConfig.CurrentThemeType; @@ -37,6 +41,22 @@ public class WindowHelper /// 要应用主题的窗口 /// 主题类型 public static void ApplyThemeToWindow(System.Windows.Window window, ThemeType themeType) + { + try + { + ApplyThemeCore(window, themeType); + } + catch (COMException ex) when ((uint)ex.HResult == DesktopCompositionDisabledHResult) + { + ApplyFallbackTheme(window, themeType); + } + catch + { + ApplyFallbackTheme(window, themeType); + } + } + + private static void ApplyThemeCore(System.Windows.Window window, ThemeType themeType) { switch (themeType) { @@ -76,4 +96,21 @@ public class WindowHelper break; } } -} \ No newline at end of file + + private static void ApplyFallbackTheme(System.Windows.Window window, ThemeType themeType) + { + window.Background = new SolidColorBrush(GetFallbackBackgroundColor(themeType)); + WindowBackdrop.ApplyBackdrop(window, WindowBackdropType.None); + } + + private static Color GetFallbackBackgroundColor(ThemeType themeType) + { + return themeType switch + { + ThemeType.LightNone => Color.FromArgb(255, 243, 243, 243), + ThemeType.LightMica => Color.FromArgb(255, 243, 243, 243), + ThemeType.LightAcrylic => Color.FromArgb(255, 243, 243, 243), + _ => Color.FromArgb(255, 32, 32, 32) + }; + } +}