Skip to content

Commit 11afa2b

Browse files
authored
Merge pull request #4058 from MahApps/fix/GH-4013
Fix NullReferenceException in WindowsSettingBehavior
2 parents 7171d3a + 1fe3a59 commit 11afa2b

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ private void AssociatedObject_SourceInitialized(object sender, EventArgs e)
4747
window.Closed += this.AssociatedObject_Closed;
4848

4949
// This operation must be thread safe
50-
Application.Current?.BeginInvoke(() => Application.Current.SessionEnding += this.CurrentApplicationSessionEnding);
50+
window.BeginInvoke(() =>
51+
{
52+
var application = Application.Current;
53+
if (application != null)
54+
{
55+
application.SessionEnding += this.CurrentApplicationSessionEnding;
56+
}
57+
});
5158
}
5259

5360
private void AssociatedObject_Closing(object sender, System.ComponentModel.CancelEventArgs e)
@@ -93,7 +100,14 @@ private void CleanUp(string fromWhere)
93100
window.SourceInitialized -= this.AssociatedObject_SourceInitialized;
94101

95102
// This operation must be thread safe
96-
Application.Current?.BeginInvoke(() => Application.Current.SessionEnding -= this.CurrentApplicationSessionEnding);
103+
window.BeginInvoke(() =>
104+
{
105+
var application = Application.Current;
106+
if (application != null)
107+
{
108+
application.SessionEnding -= this.CurrentApplicationSessionEnding;
109+
}
110+
});
97111
}
98112

99113
#pragma warning disable 618

0 commit comments

Comments
 (0)