From 05b605ef279eca8e0a8a67d77f1440e7d54fa2ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pol=C3=A1=C5=A1ek?= Date: Mon, 13 Oct 2025 19:03:38 +0200 Subject: [PATCH] CmdPal: Cleanup content page view model when no longer needed (#42293) ## Summary of the Pull Request This PR: - Cleans up ContentPageViewModel when its page unloads to ensure it unsubscribes from ItemsChanged. - Clears the command bar before initializing a new page view model, allowing the new VM to set its own state without being overridden by the shell afterward. ## PR Checklist - [x] Closes: #42291 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed --- .../Core/Microsoft.CmdPal.Core.ViewModels/ShellViewModel.cs | 6 +++--- .../cmdpal/Microsoft.CmdPal.UI/ExtViews/ContentPage.xaml.cs | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) 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; }