From 1fe3a594e659df9e0cf3ff71f03bd9312eab659c Mon Sep 17 00:00:00 2001 From: punker76 Date: Sun, 28 Feb 2021 21:18:39 +0100 Subject: [PATCH] (GH-4013) Fix NullReferenceException in WindowsSettingBehavior --- .../Behaviors/WindowsSettingBehavior.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs b/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs index 4580d35a4a..d144e11e6d 100644 --- a/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs +++ b/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs @@ -47,7 +47,14 @@ private void AssociatedObject_SourceInitialized(object sender, EventArgs e) window.Closed += this.AssociatedObject_Closed; // This operation must be thread safe - Application.Current?.BeginInvoke(() => Application.Current.SessionEnding += this.CurrentApplicationSessionEnding); + window.BeginInvoke(() => + { + var application = Application.Current; + if (application != null) + { + application.SessionEnding += this.CurrentApplicationSessionEnding; + } + }); } private void AssociatedObject_Closing(object sender, System.ComponentModel.CancelEventArgs e) @@ -93,7 +100,14 @@ private void CleanUp(string fromWhere) window.SourceInitialized -= this.AssociatedObject_SourceInitialized; // This operation must be thread safe - Application.Current?.BeginInvoke(() => Application.Current.SessionEnding -= this.CurrentApplicationSessionEnding); + window.BeginInvoke(() => + { + var application = Application.Current; + if (application != null) + { + application.SessionEnding -= this.CurrentApplicationSessionEnding; + } + }); } #pragma warning disable 618