Skip to content

Commit

Permalink
BugFix: BasemapGallery fails to load default Basemaps in WPF (#586)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
prathameshnarkhede authored Jul 25, 2024
1 parent 33e29d2 commit 2fc9789
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Esri.ArcGISRuntime.Toolkit.UI.Controls
public partial class BasemapGallery : Control
{
private readonly BasemapGalleryController _controller;
private bool isAvailableBasemapCollectionInitialized;

/// <summary>
/// Initializes a new instance of the <see cref="BasemapGallery"/> class.
Expand All @@ -44,6 +45,7 @@ public BasemapGallery()
DefaultStyleKey = typeof(BasemapGallery);
SizeChanged += BasemapGallerySizeChanged;
AvailableBasemaps = new ObservableCollection<BasemapGalleryItem>();
isAvailableBasemapCollectionInitialized = true;
_controller.PropertyChanged += HandleControllerPropertyChanged;
Loaded += BasemapGallery_Loaded;
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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<BasemapGalleryItem>;
Expand Down

0 comments on commit 2fc9789

Please # to comment.