diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ShellViewModel.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ShellViewModel.cs index 40afae6f9c..41db974f5b 100644 --- a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ShellViewModel.cs +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ShellViewModel.cs @@ -265,6 +265,9 @@ public partial class ShellViewModel : ObservableObject, throw new NotSupportedException(); } + // Clear command bar, ViewModel initialization can already set new commands if it wants to + OnUIThread(() => WeakReferenceMessenger.Default.Send(new(null))); + // Kick off async loading of our ViewModel LoadPageViewModelAsync(pageViewModel, navigationToken) .ContinueWith( @@ -275,9 +278,6 @@ public partial class ShellViewModel : ObservableObject, { newCts.Dispose(); } - - // When we're done loading the page, then update the command bar to match - WeakReferenceMessenger.Default.Send(new(null)); }, navigationToken, TaskContinuationOptions.None, 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 e9ab57cfa5..c022d82b34 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ContentPage.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ContentPage.xaml.cs @@ -78,6 +78,12 @@ public sealed partial class ContentPage : Page, WeakReferenceMessenger.Default.Unregister(this); // Clean-up event listeners + if (e.NavigationMode != NavigationMode.New) + { + ViewModel?.SafeCleanup(); + CleanupHelper.Cleanup(this); + } + ViewModel = null; }