From 74bdc8afbdf277b0e29803e27a83970ea856bf40 Mon Sep 17 00:00:00 2001 From: Dinesh Solanki <15937452+DineshSolanki@users.noreply.github.com> Date: Thu, 18 Jun 2026 00:49:25 +0530 Subject: [PATCH] fix: prevent TypeInitializationException crash in Window static constructor The Window static constructor loaded a 14K-line ResourceDictionary (Theme.xaml) via GetResourceInternal, which could throw ArgumentOutOfRangeException from WPF internals when Application.Current was null or resources were not fully initialized. Once the static constructor failed, .NET cached the TypeInitializationException permanently, making all subsequent Window/MessageBox instantiations crash for the lifetime of the AppDomain. Changes: - ResourceHelper: use Lazy for thread-safe theme initialization; add Application.Current.TryFindResource fallback in GetResourceInternal when theme dictionary lookup fails or is null - Window: wrap static constructor in try-catch so a resource loading failure falls back to a null style instead of poisoning the type permanently Fixes #442 --- .../HandyControl_Shared/Controls/Window/Window.cs | 9 ++++++++- .../Tools/Helper/ResourceHelper.cs | 12 ++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Window/Window.cs b/src/Shared/HandyControl_Shared/Controls/Window/Window.cs index 7de0ebbe2..76e617d56 100644 --- a/src/Shared/HandyControl_Shared/Controls/Window/Window.cs +++ b/src/Shared/HandyControl_Shared/Controls/Window/Window.cs @@ -49,7 +49,14 @@ public class Window : System.Windows.Window static Window() { - StyleProperty.OverrideMetadata(typeof(Window), new FrameworkPropertyMetadata(ResourceHelper.GetResourceInternal