diff --git a/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs b/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs index d144e11e6d..c9bf1edb1a 100644 --- a/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs +++ b/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs @@ -46,15 +46,14 @@ private void AssociatedObject_SourceInitialized(object sender, EventArgs e) window.Closing += this.AssociatedObject_Closing; window.Closed += this.AssociatedObject_Closed; - // This operation must be thread safe - window.BeginInvoke(() => + // This operation must be thread safe. It is possible, that the window is running in a different Thread. + Application.Current?.BeginInvoke(app => { - var application = Application.Current; - if (application != null) + if (app != null) { - application.SessionEnding += this.CurrentApplicationSessionEnding; + app.SessionEnding += this.CurrentApplicationSessionEnding; } - }); + }); } private void AssociatedObject_Closing(object sender, System.ComponentModel.CancelEventArgs e) @@ -100,12 +99,11 @@ private void CleanUp(string fromWhere) window.SourceInitialized -= this.AssociatedObject_SourceInitialized; // This operation must be thread safe - window.BeginInvoke(() => + Application.Current?.BeginInvoke(app => { - var application = Application.Current; - if (application != null) + if (app != null) { - application.SessionEnding -= this.CurrentApplicationSessionEnding; + app.SessionEnding -= this.CurrentApplicationSessionEnding; } }); } diff --git a/src/MahApps.Metro/Controls/Extensions.cs b/src/MahApps.Metro/Controls/Extensions.cs index c90df105ba..534bc1ee65 100644 --- a/src/MahApps.Metro/Controls/Extensions.cs +++ b/src/MahApps.Metro/Controls/Extensions.cs @@ -73,7 +73,7 @@ public static void BeginInvoke([NotNull] this DispatcherObject dispatcherObject, throw new ArgumentNullException(nameof(invokeAction)); } - dispatcherObject.Dispatcher.BeginInvoke(priority, invokeAction); + dispatcherObject.Dispatcher?.BeginInvoke(priority, invokeAction); } public static void BeginInvoke([NotNull] this T dispatcherObject, [NotNull] Action invokeAction, DispatcherPriority priority = DispatcherPriority.Background) diff --git a/src/MahApps.Metro/Themes/DateTimePicker.xaml b/src/MahApps.Metro/Themes/DateTimePicker.xaml index 8c86d0542d..1e630079fe 100644 --- a/src/MahApps.Metro/Themes/DateTimePicker.xaml +++ b/src/MahApps.Metro/Themes/DateTimePicker.xaml @@ -441,7 +441,6 @@ - @@ -474,10 +473,10 @@ - + - +