Skip to content

Commit

Permalink
Use Dispatcher to register MainWindow OnClosed event handler
Browse files Browse the repository at this point in the history
Fixes WPF designer crashes when trying to expand the Miscellaneous properties.

Using debugger, we can learn that "Application.Current?.MainWindow" throws an exception as the calling thread cannot access MainWindow, because a different thread owns it.
  • Loading branch information
echoix authored and JohanLarsson committed Sep 6, 2024
1 parent 49e0ce4 commit 51e6571
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Gu.Wpf.NumericInput/Touch/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ static TextBox()
typeof(System.Windows.Controls.TextBox),
UIElement.LostFocusEvent,
new RoutedEventHandler(OnLostFocus));
var mainWindow = Application.Current?.MainWindow;
if (mainWindow != null)
Application.Current.Dispatcher.Invoke(() =>
{
mainWindow.Closed += OnClosed;
}
var mainWindow = Application.Current?.MainWindow;
if (mainWindow != null)
{
mainWindow.Closed += OnClosed;
}
});
}

/// <summary>Helper for setting <see cref="ShowTouchKeyboardOnTouchEnterProperty"/> on <paramref name="element"/>.</summary>
Expand Down

0 comments on commit 51e6571

Please sign in to comment.