From 972bd091bcce901a8fa99973db1440cb61cc572a Mon Sep 17 00:00:00 2001 From: Violet Hansen Date: Sun, 28 Sep 2025 21:06:01 +0300 Subject: [PATCH 1/8] Fixed a memory leak issue related to event handlers Event handlers in the HoverLight weren't being unsubscribed On Disconnected. --- AIDevGallery/Controls/HomePage/Header/Lights/HoverLight.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/AIDevGallery/Controls/HomePage/Header/Lights/HoverLight.cs b/AIDevGallery/Controls/HomePage/Header/Lights/HoverLight.cs index 6e67b2ba..ab8f77e0 100644 --- a/AIDevGallery/Controls/HomePage/Header/Lights/HoverLight.cs +++ b/AIDevGallery/Controls/HomePage/Header/Lights/HoverLight.cs @@ -92,6 +92,10 @@ private void TargetElement_PointerExited(object sender, PointerRoutedEventArgs e protected override void OnDisconnected(UIElement oldElement) { + // Unsubscribe event handlers to avoid retaining references and leaking the light or target element + oldElement.PointerMoved -= TargetElement_PointerMoved; + oldElement.PointerExited -= TargetElement_PointerExited; + // Dispose Light and Composition resources when it is removed from the tree RemoveTargetElement(GetId(), oldElement); CompositionLight.Dispose(); @@ -104,4 +108,4 @@ protected override string GetId() { return Id; } -} \ No newline at end of file +} From 8cde6c84f178cd6e6d082572e5e57db335ffeb78 Mon Sep 17 00:00:00 2001 From: Violet Hansen Date: Sun, 28 Sep 2025 21:19:48 +0300 Subject: [PATCH 2/8] Fixed memory management issues in OpacityMaskView OpacityMaskView wasn't handling resources properly on unload, causing memory leaks and also changing ```csharp private static CompositionBrush GetVisualBrush(UIElement element) ``` to ```csharp private static CompositionSurfaceBrush GetVisualBrush(UIElement element) ``` improves performance. --- AIDevGallery/Controls/OpacityMask.xaml.cs | 72 ++++++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/AIDevGallery/Controls/OpacityMask.xaml.cs b/AIDevGallery/Controls/OpacityMask.xaml.cs index feb92c7e..20b860c8 100644 --- a/AIDevGallery/Controls/OpacityMask.xaml.cs +++ b/AIDevGallery/Controls/OpacityMask.xaml.cs @@ -31,8 +31,13 @@ public partial class OpacityMaskView : ContentControl private const string RootGridTemplateName = "PART_RootGrid"; private readonly Compositor _compositor = CompositionTarget.GetCompositorForCurrentThread(); + + // Composition resources we create and need to tear down explicitly to avoid retaining composition references longer than necessary private CompositionBrush? _mask; private CompositionMaskBrush? _maskBrush; + private CompositionSurfaceBrush? _sourceBrush; + private Grid? _rootGrid; + private SpriteVisual? _redirectVisual; /// /// Initializes a new instance of the class. @@ -41,6 +46,9 @@ public partial class OpacityMaskView : ContentControl public OpacityMaskView() { DefaultStyleKey = typeof(OpacityMaskView); + + // Ensure composition resources are cleaned up when control unloads + Unloaded += OpacityMaskView_Unloaded; } /// @@ -57,22 +65,35 @@ protected override void OnApplyTemplate() { base.OnApplyTemplate(); + // Clean up any prior composition resources (e.g., when the template is re-applied) + CleanupComposition(); + Grid rootGrid = (Grid)GetTemplateChild(RootGridTemplateName); ContentPresenter contentPresenter = (ContentPresenter)GetTemplateChild(ContentPresenterTemplateName); Border maskContainer = (Border)GetTemplateChild(MaskContainerTemplateName); + _rootGrid = rootGrid; + + // Create mask brush and its sources _maskBrush = _compositor.CreateMaskBrush(); - _maskBrush.Source = GetVisualBrush(contentPresenter); + + // Source is the content we want to render through the mask + _sourceBrush = GetVisualBrush(contentPresenter); + _maskBrush.Source = _sourceBrush; + + // Mask is the opacity mask visual brush _mask = GetVisualBrush(maskContainer); _maskBrush.Mask = OpacityMask is null ? null : _mask; + // Create a sprite visual that draws with the mask brush, and redirect the control visual to it SpriteVisual redirectVisual = _compositor.CreateSpriteVisual(); redirectVisual.RelativeSizeAdjustment = Vector2.One; redirectVisual.Brush = _maskBrush; ElementCompositionPreview.SetElementChildVisual(rootGrid, redirectVisual); + _redirectVisual = redirectVisual; } - private static CompositionBrush GetVisualBrush(UIElement element) + private static CompositionSurfaceBrush GetVisualBrush(UIElement element) { Visual visual = ElementCompositionPreview.GetElementVisual(element); @@ -100,6 +121,51 @@ private static void OnOpacityMaskChanged(DependencyObject d, DependencyPropertyC } UIElement? opacityMask = (UIElement?)e.NewValue; + + // Switch to the mask brush if an opacity mask is set; otherwise remove the mask maskBrush.Mask = opacityMask is null ? null : self._mask; } -} \ No newline at end of file + + // On control unload, ensure we tear down composition resources and clear the child visual + private void OpacityMaskView_Unloaded(object sender, RoutedEventArgs e) => CleanupComposition(); + + /// + /// Clears the ElementCompositionPreview child visual and disposes all composition resources created by this control. + /// This prevents composition resource retention across template reapplications or when the control is unloaded. + /// + private void CleanupComposition() + { + // Detach the child visual from the root grid if present + if (_rootGrid != null) + { + ElementCompositionPreview.SetElementChildVisual(_rootGrid, null); + } + + // Dispose the redirect visual + if (_redirectVisual != null) + { + _redirectVisual.Brush = null; + _redirectVisual.Dispose(); + _redirectVisual = null; + } + + // Dispose mask brush and its sources + if (_maskBrush != null) + { + _maskBrush.Source = null; + _maskBrush.Mask = null; + _maskBrush.Dispose(); + _maskBrush = null; + } + + // Dispose the source content brush explicitly + _sourceBrush?.Dispose(); + _sourceBrush = null; + + // Dispose the mask brush instance if we created one + _mask?.Dispose(); + _mask = null; + + _rootGrid = null; + } +} From 70b5416c6fb7c0fee38dfee444f0b6cdca092588 Mon Sep 17 00:00:00 2001 From: Violet Hansen Date: Sun, 28 Sep 2025 21:24:51 +0300 Subject: [PATCH 3/8] Revert "Fixed memory management issues in OpacityMaskView" This reverts commit 8cde6c84f178cd6e6d082572e5e57db335ffeb78. --- AIDevGallery/Controls/OpacityMask.xaml.cs | 72 +---------------------- 1 file changed, 3 insertions(+), 69 deletions(-) diff --git a/AIDevGallery/Controls/OpacityMask.xaml.cs b/AIDevGallery/Controls/OpacityMask.xaml.cs index 20b860c8..feb92c7e 100644 --- a/AIDevGallery/Controls/OpacityMask.xaml.cs +++ b/AIDevGallery/Controls/OpacityMask.xaml.cs @@ -31,13 +31,8 @@ public partial class OpacityMaskView : ContentControl private const string RootGridTemplateName = "PART_RootGrid"; private readonly Compositor _compositor = CompositionTarget.GetCompositorForCurrentThread(); - - // Composition resources we create and need to tear down explicitly to avoid retaining composition references longer than necessary private CompositionBrush? _mask; private CompositionMaskBrush? _maskBrush; - private CompositionSurfaceBrush? _sourceBrush; - private Grid? _rootGrid; - private SpriteVisual? _redirectVisual; /// /// Initializes a new instance of the class. @@ -46,9 +41,6 @@ public partial class OpacityMaskView : ContentControl public OpacityMaskView() { DefaultStyleKey = typeof(OpacityMaskView); - - // Ensure composition resources are cleaned up when control unloads - Unloaded += OpacityMaskView_Unloaded; } /// @@ -65,35 +57,22 @@ protected override void OnApplyTemplate() { base.OnApplyTemplate(); - // Clean up any prior composition resources (e.g., when the template is re-applied) - CleanupComposition(); - Grid rootGrid = (Grid)GetTemplateChild(RootGridTemplateName); ContentPresenter contentPresenter = (ContentPresenter)GetTemplateChild(ContentPresenterTemplateName); Border maskContainer = (Border)GetTemplateChild(MaskContainerTemplateName); - _rootGrid = rootGrid; - - // Create mask brush and its sources _maskBrush = _compositor.CreateMaskBrush(); - - // Source is the content we want to render through the mask - _sourceBrush = GetVisualBrush(contentPresenter); - _maskBrush.Source = _sourceBrush; - - // Mask is the opacity mask visual brush + _maskBrush.Source = GetVisualBrush(contentPresenter); _mask = GetVisualBrush(maskContainer); _maskBrush.Mask = OpacityMask is null ? null : _mask; - // Create a sprite visual that draws with the mask brush, and redirect the control visual to it SpriteVisual redirectVisual = _compositor.CreateSpriteVisual(); redirectVisual.RelativeSizeAdjustment = Vector2.One; redirectVisual.Brush = _maskBrush; ElementCompositionPreview.SetElementChildVisual(rootGrid, redirectVisual); - _redirectVisual = redirectVisual; } - private static CompositionSurfaceBrush GetVisualBrush(UIElement element) + private static CompositionBrush GetVisualBrush(UIElement element) { Visual visual = ElementCompositionPreview.GetElementVisual(element); @@ -121,51 +100,6 @@ private static void OnOpacityMaskChanged(DependencyObject d, DependencyPropertyC } UIElement? opacityMask = (UIElement?)e.NewValue; - - // Switch to the mask brush if an opacity mask is set; otherwise remove the mask maskBrush.Mask = opacityMask is null ? null : self._mask; } - - // On control unload, ensure we tear down composition resources and clear the child visual - private void OpacityMaskView_Unloaded(object sender, RoutedEventArgs e) => CleanupComposition(); - - /// - /// Clears the ElementCompositionPreview child visual and disposes all composition resources created by this control. - /// This prevents composition resource retention across template reapplications or when the control is unloaded. - /// - private void CleanupComposition() - { - // Detach the child visual from the root grid if present - if (_rootGrid != null) - { - ElementCompositionPreview.SetElementChildVisual(_rootGrid, null); - } - - // Dispose the redirect visual - if (_redirectVisual != null) - { - _redirectVisual.Brush = null; - _redirectVisual.Dispose(); - _redirectVisual = null; - } - - // Dispose mask brush and its sources - if (_maskBrush != null) - { - _maskBrush.Source = null; - _maskBrush.Mask = null; - _maskBrush.Dispose(); - _maskBrush = null; - } - - // Dispose the source content brush explicitly - _sourceBrush?.Dispose(); - _sourceBrush = null; - - // Dispose the mask brush instance if we created one - _mask?.Dispose(); - _mask = null; - - _rootGrid = null; - } -} +} \ No newline at end of file From b0af3ab94ae073ac9326f1047020a6b3ee0d53c3 Mon Sep 17 00:00:00 2001 From: Violet Hansen Date: Sun, 28 Sep 2025 21:30:13 +0300 Subject: [PATCH 4/8] Revert "Fixed a memory leak issue related to event handlers" This reverts commit 972bd091bcce901a8fa99973db1440cb61cc572a. --- AIDevGallery/Controls/HomePage/Header/Lights/HoverLight.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/AIDevGallery/Controls/HomePage/Header/Lights/HoverLight.cs b/AIDevGallery/Controls/HomePage/Header/Lights/HoverLight.cs index ab8f77e0..6e67b2ba 100644 --- a/AIDevGallery/Controls/HomePage/Header/Lights/HoverLight.cs +++ b/AIDevGallery/Controls/HomePage/Header/Lights/HoverLight.cs @@ -92,10 +92,6 @@ private void TargetElement_PointerExited(object sender, PointerRoutedEventArgs e protected override void OnDisconnected(UIElement oldElement) { - // Unsubscribe event handlers to avoid retaining references and leaking the light or target element - oldElement.PointerMoved -= TargetElement_PointerMoved; - oldElement.PointerExited -= TargetElement_PointerExited; - // Dispose Light and Composition resources when it is removed from the tree RemoveTargetElement(GetId(), oldElement); CompositionLight.Dispose(); @@ -108,4 +104,4 @@ protected override string GetId() { return Id; } -} +} \ No newline at end of file From e7146536ffb56cef487cd9960ff5031d5d19314a Mon Sep 17 00:00:00 2001 From: Violet Hansen Date: Sun, 28 Sep 2025 21:53:17 +0300 Subject: [PATCH 5/8] Fix home page carousel visually leaking to other pages after navigation The carousel on the home page visually leaks to other pages after navigation, that means we can see the carousel on other pages briefly after clicking on the tiles in the home page carousel. This PR fixes this problem completely, however, you need to accept my previous PR first, otherwise this change won't work: https://github.com/microsoft/ai-dev-gallery/pull/458/files --- AIDevGallery/Pages/HomePage.xaml.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/AIDevGallery/Pages/HomePage.xaml.cs b/AIDevGallery/Pages/HomePage.xaml.cs index f0712b78..7da68166 100644 --- a/AIDevGallery/Pages/HomePage.xaml.cs +++ b/AIDevGallery/Pages/HomePage.xaml.cs @@ -26,6 +26,23 @@ protected override void OnNavigatedTo(NavigationEventArgs e) NavigatedToPageEvent.Log(nameof(HomePage)); } + protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) + { + // Remove all UI elements to trigger Unloaded and composition cleanup + if (this.Content is Grid rootGrid) + { + rootGrid.Children.Clear(); + } + + // Clear any MRU data + mostRecentlyUsedItems.Clear(); + + // Remove all content to guarantee nothing can be rendered + this.Content = null; + + base.OnNavigatingFrom(e); + } + private void Page_Loaded(object sender, RoutedEventArgs e) { if (!App.AppData.IsDiagnosticsMessageDismissed && PrivacyConsentHelpers.IsPrivacySensitiveRegion()) From 4ec54e88fe6c57aa61ccf8fa9175887bbf82537f Mon Sep 17 00:00:00 2001 From: Violet Hansen Date: Wed, 15 Oct 2025 16:53:48 +0300 Subject: [PATCH 6/8] Ran dotnet format as requested Ran dotnet format as requested --- AIDevGallery/Controls/HomePage/Header/AnimatedImage.xaml.cs | 1 + .../ModelPicker/ModelPickerViews/ModelPickerDefinitions.cs | 1 + .../Controls/ModelPicker/ModelPickerViews/OnnxPickerView.xaml.cs | 1 + AIDevGallery/Controls/WcrModelDownloader.xaml.cs | 1 + AIDevGallery/ExternalModelUtils/FoundryLocal/Utils.cs | 1 + AIDevGallery/Pages/APIs/APIOverview.xaml.cs | 1 + AIDevGallery/Samples/Definitions/WcrApis/WcrApiHelpers.cs | 1 + AIDevGallery/Samples/SharedCode/WinMLHelpers.cs | 1 + AIDevGallery/Samples/WCRAPIs/PhiSilicaBasic.xaml.cs | 1 + AIDevGallery/Samples/WCRAPIs/PhiSilicaLoRa.xaml.cs | 1 + AIDevGallery/Samples/WCRAPIs/TextRewrite.xaml.cs | 1 + AIDevGallery/Samples/WCRAPIs/TextSummarize.xaml.cs | 1 + AIDevGallery/Samples/WCRAPIs/TextToTable.xaml.cs | 1 + 13 files changed, 13 insertions(+) diff --git a/AIDevGallery/Controls/HomePage/Header/AnimatedImage.xaml.cs b/AIDevGallery/Controls/HomePage/Header/AnimatedImage.xaml.cs index 2dcfc524..06fffe30 100644 --- a/AIDevGallery/Controls/HomePage/Header/AnimatedImage.xaml.cs +++ b/AIDevGallery/Controls/HomePage/Header/AnimatedImage.xaml.cs @@ -8,6 +8,7 @@ using System; namespace AIDevGallery.Controls; + internal partial class AnimatedImage : UserControl { private AnimationSet? selectAnimation; diff --git a/AIDevGallery/Controls/ModelPicker/ModelPickerViews/ModelPickerDefinitions.cs b/AIDevGallery/Controls/ModelPicker/ModelPickerViews/ModelPickerDefinitions.cs index 7fa22451..ff2d9fab 100644 --- a/AIDevGallery/Controls/ModelPicker/ModelPickerViews/ModelPickerDefinitions.cs +++ b/AIDevGallery/Controls/ModelPicker/ModelPickerViews/ModelPickerDefinitions.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; namespace AIDevGallery.Controls.ModelPickerViews; + internal class ModelPickerDefinition { public static readonly Dictionary Definitions = new() diff --git a/AIDevGallery/Controls/ModelPicker/ModelPickerViews/OnnxPickerView.xaml.cs b/AIDevGallery/Controls/ModelPicker/ModelPickerViews/OnnxPickerView.xaml.cs index 9255954a..a2810589 100644 --- a/AIDevGallery/Controls/ModelPicker/ModelPickerViews/OnnxPickerView.xaml.cs +++ b/AIDevGallery/Controls/ModelPicker/ModelPickerViews/OnnxPickerView.xaml.cs @@ -18,6 +18,7 @@ using Windows.ApplicationModel.DataTransfer; namespace AIDevGallery.Controls.ModelPickerViews; + internal sealed partial class OnnxPickerView : BaseModelPickerView { private List models = []; diff --git a/AIDevGallery/Controls/WcrModelDownloader.xaml.cs b/AIDevGallery/Controls/WcrModelDownloader.xaml.cs index 83efd98c..548fa33c 100644 --- a/AIDevGallery/Controls/WcrModelDownloader.xaml.cs +++ b/AIDevGallery/Controls/WcrModelDownloader.xaml.cs @@ -15,6 +15,7 @@ using Windows.System; namespace AIDevGallery.Controls; + internal sealed partial class WcrModelDownloader : UserControl { public event EventHandler? DownloadClicked; diff --git a/AIDevGallery/ExternalModelUtils/FoundryLocal/Utils.cs b/AIDevGallery/ExternalModelUtils/FoundryLocal/Utils.cs index 104cc6fd..22c98504 100644 --- a/AIDevGallery/ExternalModelUtils/FoundryLocal/Utils.cs +++ b/AIDevGallery/ExternalModelUtils/FoundryLocal/Utils.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; namespace AIDevGallery.ExternalModelUtils.FoundryLocal; + internal class Utils { public async static Task<(string? Output, string? Error, int ExitCode)> RunFoundryWithArguments(string arguments) diff --git a/AIDevGallery/Pages/APIs/APIOverview.xaml.cs b/AIDevGallery/Pages/APIs/APIOverview.xaml.cs index 65afdb7f..4c170089 100644 --- a/AIDevGallery/Pages/APIs/APIOverview.xaml.cs +++ b/AIDevGallery/Pages/APIs/APIOverview.xaml.cs @@ -8,6 +8,7 @@ using System.Collections.ObjectModel; namespace AIDevGallery.Pages; + internal sealed partial class APIOverview : Page { private ObservableCollection wcrAPIs = new(); diff --git a/AIDevGallery/Samples/Definitions/WcrApis/WcrApiHelpers.cs b/AIDevGallery/Samples/Definitions/WcrApis/WcrApiHelpers.cs index d51806f8..a895630d 100644 --- a/AIDevGallery/Samples/Definitions/WcrApis/WcrApiHelpers.cs +++ b/AIDevGallery/Samples/Definitions/WcrApis/WcrApiHelpers.cs @@ -11,6 +11,7 @@ using Windows.Foundation; namespace AIDevGallery.Samples; + internal static class WcrApiHelpers { private static readonly HashSet LanguageModelBacked = new() diff --git a/AIDevGallery/Samples/SharedCode/WinMLHelpers.cs b/AIDevGallery/Samples/SharedCode/WinMLHelpers.cs index da933724..5538b473 100644 --- a/AIDevGallery/Samples/SharedCode/WinMLHelpers.cs +++ b/AIDevGallery/Samples/SharedCode/WinMLHelpers.cs @@ -8,6 +8,7 @@ using System.Linq; namespace AIDevGallery.Samples.SharedCode; + internal static class WinMLHelpers { public static bool AppendExecutionProviderFromEpName(this SessionOptions sessionOptions, string epName, string? deviceType, OrtEnv? environment = null) diff --git a/AIDevGallery/Samples/WCRAPIs/PhiSilicaBasic.xaml.cs b/AIDevGallery/Samples/WCRAPIs/PhiSilicaBasic.xaml.cs index c86dd925..26802f0d 100644 --- a/AIDevGallery/Samples/WCRAPIs/PhiSilicaBasic.xaml.cs +++ b/AIDevGallery/Samples/WCRAPIs/PhiSilicaBasic.xaml.cs @@ -20,6 +20,7 @@ */ namespace AIDevGallery.Samples.WCRAPIs; + [GallerySample( Name = "Generate with Phi Silica", Model1Types = [ModelType.PhiSilica], diff --git a/AIDevGallery/Samples/WCRAPIs/PhiSilicaLoRa.xaml.cs b/AIDevGallery/Samples/WCRAPIs/PhiSilicaLoRa.xaml.cs index 82fdb406..54795b5f 100644 --- a/AIDevGallery/Samples/WCRAPIs/PhiSilicaLoRa.xaml.cs +++ b/AIDevGallery/Samples/WCRAPIs/PhiSilicaLoRa.xaml.cs @@ -21,6 +21,7 @@ */ namespace AIDevGallery.Samples.WCRAPIs; + internal sealed partial class PhiSilicaLoRa : BaseSamplePage { internal enum GenerationType diff --git a/AIDevGallery/Samples/WCRAPIs/TextRewrite.xaml.cs b/AIDevGallery/Samples/WCRAPIs/TextRewrite.xaml.cs index 9be98449..7ac5fd79 100644 --- a/AIDevGallery/Samples/WCRAPIs/TextRewrite.xaml.cs +++ b/AIDevGallery/Samples/WCRAPIs/TextRewrite.xaml.cs @@ -20,6 +20,7 @@ */ namespace AIDevGallery.Samples.WCRAPIs; + [GallerySample( Name = "Windows AI TextRewriter", Model1Types = [ModelType.TextRewriter], diff --git a/AIDevGallery/Samples/WCRAPIs/TextSummarize.xaml.cs b/AIDevGallery/Samples/WCRAPIs/TextSummarize.xaml.cs index 468b75dc..6aaeb5e4 100644 --- a/AIDevGallery/Samples/WCRAPIs/TextSummarize.xaml.cs +++ b/AIDevGallery/Samples/WCRAPIs/TextSummarize.xaml.cs @@ -20,6 +20,7 @@ */ namespace AIDevGallery.Samples.WCRAPIs; + [GallerySample( Name = "Windows AI Text Summarizer", Model1Types = [ModelType.TextSummarizer], diff --git a/AIDevGallery/Samples/WCRAPIs/TextToTable.xaml.cs b/AIDevGallery/Samples/WCRAPIs/TextToTable.xaml.cs index 36d0cd61..5ab06a9c 100644 --- a/AIDevGallery/Samples/WCRAPIs/TextToTable.xaml.cs +++ b/AIDevGallery/Samples/WCRAPIs/TextToTable.xaml.cs @@ -21,6 +21,7 @@ */ namespace AIDevGallery.Samples.WCRAPIs; + [GallerySample( Name = "Windows AI Text To Table Converter", Model1Types = [ModelType.TextToTableConverter], From bd4ab3d6c97057435e680c8095f4504f7441ccf9 Mon Sep 17 00:00:00 2001 From: Violet Hansen Date: Thu, 16 Oct 2025 18:00:45 +0300 Subject: [PATCH 7/8] Update HomePage.xaml.cs --- AIDevGallery/Pages/HomePage.xaml.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/AIDevGallery/Pages/HomePage.xaml.cs b/AIDevGallery/Pages/HomePage.xaml.cs index 7da68166..ae7afa34 100644 --- a/AIDevGallery/Pages/HomePage.xaml.cs +++ b/AIDevGallery/Pages/HomePage.xaml.cs @@ -34,9 +34,6 @@ protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) rootGrid.Children.Clear(); } - // Clear any MRU data - mostRecentlyUsedItems.Clear(); - // Remove all content to guarantee nothing can be rendered this.Content = null; From 158769dcf2f95683ed2152239c0609e49757e351 Mon Sep 17 00:00:00 2001 From: Violet Hansen Date: Thu, 16 Oct 2025 18:10:47 +0300 Subject: [PATCH 8/8] Updated the cleanup code as requested. Updated the cleanup code as requested. --- AIDevGallery/Pages/HomePage.xaml.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/AIDevGallery/Pages/HomePage.xaml.cs b/AIDevGallery/Pages/HomePage.xaml.cs index ae7afa34..5294db35 100644 --- a/AIDevGallery/Pages/HomePage.xaml.cs +++ b/AIDevGallery/Pages/HomePage.xaml.cs @@ -28,13 +28,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { - // Remove all UI elements to trigger Unloaded and composition cleanup - if (this.Content is Grid rootGrid) - { - rootGrid.Children.Clear(); - } - - // Remove all content to guarantee nothing can be rendered + // Remove all content to guarantee nothing can be rendered; reference issue: https://github.com/microsoft/ai-dev-gallery/pull/460 this.Content = null; base.OnNavigatingFrom(e);