From 1f2c020ff70cc8b70abc9e70f6bb0d987f0786fd Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede Date: Wed, 4 Sep 2024 16:46:45 -0700 Subject: [PATCH] Refactor BookmarksView for simplicity and performance --- .../BookmarksView/BookmarksView.cs | 42 +++++-------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/src/Toolkit/Toolkit.Maui/BookmarksView/BookmarksView.cs b/src/Toolkit/Toolkit.Maui/BookmarksView/BookmarksView.cs index 59342b348..b428968ec 100644 --- a/src/Toolkit/Toolkit.Maui/BookmarksView/BookmarksView.cs +++ b/src/Toolkit/Toolkit.Maui/BookmarksView/BookmarksView.cs @@ -24,7 +24,7 @@ namespace Esri.ArcGISRuntime.Toolkit.Maui; /// public class BookmarksView : TemplatedView { - private BookmarksViewDataSource _dataSource = new BookmarksViewDataSource(); + private readonly BookmarksViewDataSource _dataSource = new(); private const string _presentingViewName = "PresentingView"; private static readonly DataTemplate DefaultDataTemplate; @@ -33,18 +33,17 @@ public class BookmarksView : TemplatedView [DynamicDependency(nameof(Bookmark.Name), "Esri.ArcGISRuntime.Mapping.Bookmark", "Esri.ArcGISRuntime")] static BookmarksView() { - DefaultDataTemplate = new DataTemplate(() => - { - var defaultLabel = new Label(); - defaultLabel.SetBinding(Label.TextProperty, nameof(Bookmark.Name)); - return defaultLabel; - }); + string dataTemplate = + @" + "; + DefaultDataTemplate = Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(new DataTemplate(), dataTemplate); string template = $@" - + xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" + xmlns:esriTK=""clr-namespace:Esri.ArcGISRuntime.Toolkit.Maui""> + "; DefaultControlTemplate = Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(new ControlTemplate(), template); } @@ -70,8 +69,8 @@ protected override void OnApplyTemplate() { view.SelectionChanged -= Internal_bookmarkSelected; view.SelectionChanged += Internal_bookmarkSelected; + view.ItemsSource = _dataSource; } - UpdatePresentingView(); } /// @@ -123,7 +122,7 @@ public GeoView? GeoView /// Identifies the bindable property. /// public static readonly BindableProperty ItemTemplateProperty = - BindableProperty.Create(nameof(ItemTemplate), typeof(DataTemplate), typeof(BookmarksView), DefaultDataTemplate, BindingMode.OneWay, null, propertyChanged: ItemTemplateChanged); + BindableProperty.Create(nameof(ItemTemplate), typeof(DataTemplate), typeof(BookmarksView), DefaultDataTemplate, BindingMode.OneWay, null); /// /// Handles property changes for the bindable property. @@ -143,15 +142,6 @@ private static void GeoViewChanged(BindableObject sender, object? oldValue, obje bookmarkView._dataSource.SetGeoView(newValue as GeoView); } - /// - /// Handles property changes for the bindable property. - /// - private static void ItemTemplateChanged(BindableObject sender, object? oldValue, object? newValue) - { - BookmarksView bookmarksView = (BookmarksView)sender; - bookmarksView.UpdatePresentingView(); - } - /// /// Selects the bookmark and navigates to it in the associated . /// @@ -184,16 +174,6 @@ private void Internal_bookmarkSelected(object? sender, SelectionChangedEventArgs } } - private void UpdatePresentingView() - { - var collectionView = (CollectionView)GetTemplateChild(_presentingViewName); - if (collectionView is CollectionView view) - { - view.ItemTemplate = ItemTemplate; - view.ItemsSource = _dataSource; - } - } - /// /// Raised whenever a bookmark is selected. ///