From 2fc9789299343694dcca07ff454ba651a288ae74 Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede <55591622+prathameshnarkhede@users.noreply.github.com> Date: Thu, 25 Jul 2024 13:13:39 -0700 Subject: [PATCH] BugFix: BasemapGallery fails to load default Basemaps in WPF (#586) * Optimize BasemapGallery initialization Removed redundant initialization of `AvailableBasemaps` in the `BasemapGallery` constructor. Added a null check in `BasemapGallery_Loaded` to initialize `AvailableBasemaps` only if it is null, calling `_controller.LoadFromDefaultPortal()` asynchronously if needed. This change prevents unnecessary initialization and allows for external setting of `AvailableBasemaps` before the control is loaded. * Initialize AvailableBasemaps and improve load check Add initialization flag for BasemapGallery collection --- .../BasemapGallery/BasemapGallery.Windows.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs index 66ecfb5f8..314bef103 100644 --- a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs +++ b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs @@ -34,6 +34,7 @@ namespace Esri.ArcGISRuntime.Toolkit.UI.Controls public partial class BasemapGallery : Control { private readonly BasemapGalleryController _controller; + private bool isAvailableBasemapCollectionInitialized; /// /// Initializes a new instance of the class. @@ -44,6 +45,7 @@ public BasemapGallery() DefaultStyleKey = typeof(BasemapGallery); SizeChanged += BasemapGallerySizeChanged; AvailableBasemaps = new ObservableCollection(); + isAvailableBasemapCollectionInitialized = true; _controller.PropertyChanged += HandleControllerPropertyChanged; Loaded += BasemapGallery_Loaded; } @@ -52,8 +54,8 @@ private async void BasemapGallery_Loaded(object? sender, RoutedEventArgs e) { // Unsubscribe from the Loaded event to ensure this only runs once. Loaded -= BasemapGallery_Loaded; - - if (AvailableBasemaps is null) + + if ((AvailableBasemaps is null || !AvailableBasemaps.Any()) && isAvailableBasemapCollectionInitialized) { await _controller.LoadFromDefaultPortal(); } @@ -141,6 +143,7 @@ private static void AvailableBasemapsChanged(DependencyObject d, DependencyPrope { if (d is BasemapGallery gallery) { + gallery.isAvailableBasemapCollectionInitialized = false; if (e.NewValue != gallery._controller.AvailableBasemaps) { gallery._controller.AvailableBasemaps = e.NewValue as IList;