diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ContentPage.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ContentPage.xaml.cs index 55887f155d..2bc8c2a4b0 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ContentPage.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ContentPage.xaml.cs @@ -34,8 +34,14 @@ public sealed partial class ContentPage : Page, public ContentPage() { this.InitializeComponent(); - WeakReferenceMessenger.Default.Register(this); - WeakReferenceMessenger.Default.Register(this); + this.Unloaded += OnUnloaded; + } + + private void OnUnloaded(object sender, RoutedEventArgs e) + { + // Unhook from everything to ensure nothing can reach us + // between this point and our complete and utter destruction. + WeakReferenceMessenger.Default.UnregisterAll(this); } protected override void OnNavigatedTo(NavigationEventArgs e) @@ -45,6 +51,16 @@ public sealed partial class ContentPage : Page, ViewModel = vm; } + if (!WeakReferenceMessenger.Default.IsRegistered(this)) + { + WeakReferenceMessenger.Default.Register(this); + } + + if (!WeakReferenceMessenger.Default.IsRegistered(this)) + { + WeakReferenceMessenger.Default.Register(this); + } + base.OnNavigatedTo(e); } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml.cs index 9fbdb11102..9a2ca373ca 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml.cs @@ -105,6 +105,8 @@ public sealed partial class SettingsWindow : WindowEx, private void Window_Closed(object sender, WindowEventArgs args) { WeakReferenceMessenger.Default.Send(); + + WeakReferenceMessenger.Default.UnregisterAll(this); } private void PaneToggleBtn_Click(object sender, RoutedEventArgs e)