From a797b9bc8a54c4a24f930f6be31e3704b069419a Mon Sep 17 00:00:00 2001 From: Kristen Schau <47155823+krschau@users.noreply.github.com> Date: Fri, 9 Feb 2024 12:39:32 -0500 Subject: [PATCH] deactivate widgets when not on dashboard --- .../DevHome.Dashboard/ViewModels/WidgetViewModel.cs | 5 +++++ .../DevHome.Dashboard/Views/DashboardView.xaml.cs | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs b/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs index 10b90cafd6..53d769fea0 100644 --- a/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs +++ b/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs @@ -323,4 +323,9 @@ private async void HandleWidgetUpdated(Widget sender, WidgetUpdatedEventArgs arg Log.Logger()?.ReportDebug("WidgetViewModel", $"HandleWidgetUpdated for widget {sender.Id}"); await RenderWidgetFrameworkElementAsync(); } + + public void UnsubscribeFromWidgetUpdates() + { + Widget.WidgetUpdated -= HandleWidgetUpdated; + } } diff --git a/tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs b/tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs index 54fe811154..4c8792f3b7 100644 --- a/tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs +++ b/tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs @@ -18,6 +18,7 @@ using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Automation; using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Navigation; using Microsoft.Windows.Widgets; using Microsoft.Windows.Widgets.Hosts; using Windows.System; @@ -510,6 +511,17 @@ private async void WidgetControl_Drop(object sender, DragEventArgs e) Log.Logger()?.ReportDebug("DashboardView", $"Drop ended"); } + protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) + { + Log.Logger()?.ReportDebug("DashboardView", $"Leaving Dashboard, deactivating widgets."); + + // Deactivate widgets if we're not on the Dashboard. + foreach (var widget in PinnedWidgets) + { + widget.UnsubscribeFromWidgetUpdates(); + } + } + #if DEBUG private void AddResetButton(object sender, RoutedEventArgs e) {