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