From fb1c94aef7409619d2b4556a79ebed5551eb1b36 Mon Sep 17 00:00:00 2001 From: Ieuan Walker Date: Thu, 20 Jun 2024 12:15:55 +0100 Subject: [PATCH] General performance improvements (#13) --- Scr/Breadcrumb.xaml | 5 +---- Scr/Breadcrumb.xaml.cs | 50 +++++++++++++++++------------------------- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/Scr/Breadcrumb.xaml b/Scr/Breadcrumb.xaml index bccce3c..142b394 100644 --- a/Scr/Breadcrumb.xaml +++ b/Scr/Breadcrumb.xaml @@ -2,7 +2,6 @@ + Loaded="BreadCrumbContainer_Loaded" /> \ No newline at end of file diff --git a/Scr/Breadcrumb.xaml.cs b/Scr/Breadcrumb.xaml.cs index 97d3da9..1bcd65e 100644 --- a/Scr/Breadcrumb.xaml.cs +++ b/Scr/Breadcrumb.xaml.cs @@ -9,7 +9,7 @@ public partial class Breadcrumb : ContentView #region Control properties // Separator - public static readonly BindableProperty SeparatorProperty = BindableProperty.Create(nameof(Separator), typeof(ImageSource), typeof(Breadcrumb), new FontImageSource { Glyph = " / ", Color = Colors.Black, Size = 15, }); + public static readonly BindableProperty SeparatorProperty = BindableProperty.Create(nameof(Separator), typeof(ImageSource), typeof(Breadcrumb), new FontImageSource { Glyph = " / ", Color = Colors.Black, Size = 15, }, BindingMode.OneTime); public ImageSource Separator { @@ -18,7 +18,7 @@ public ImageSource Separator } // Separator height - public static readonly BindableProperty SeparatorHeightProperty = BindableProperty.Create(nameof(SeparatorHeight), typeof(double), typeof(Breadcrumb), 15d); + public static readonly BindableProperty SeparatorHeightProperty = BindableProperty.Create(nameof(SeparatorHeight), typeof(double), typeof(Breadcrumb), 15d, BindingMode.OneTime); public double SeparatorHeight { @@ -27,7 +27,7 @@ public double SeparatorHeight } // FirstBreadCrumb - public static readonly BindableProperty FirstBreadcrumbProperty = BindableProperty.Create(nameof(FirstBreadcrumb), typeof(ImageSource), typeof(Breadcrumb), null); + public static readonly BindableProperty FirstBreadcrumbProperty = BindableProperty.Create(nameof(FirstBreadcrumb), typeof(ImageSource), typeof(Breadcrumb), null, BindingMode.OneTime); public ImageSource FirstBreadcrumb { @@ -36,7 +36,7 @@ public ImageSource FirstBreadcrumb } // Scrollbar Visibility - public static readonly BindableProperty ScrollBarVisibilityProperty = BindableProperty.Create(nameof(ScrollBarVisibility), typeof(ScrollBarVisibility), typeof(Breadcrumb), ScrollBarVisibility.Never); + public static readonly BindableProperty ScrollBarVisibilityProperty = BindableProperty.Create(nameof(ScrollBarVisibility), typeof(ScrollBarVisibility), typeof(Breadcrumb), ScrollBarVisibility.Never, BindingMode.OneTime); public ScrollBarVisibility ScrollBarVisibility { @@ -45,7 +45,7 @@ public ScrollBarVisibility ScrollBarVisibility } // FontSize - public static readonly BindableProperty FontSizeProperty = BindableProperty.Create(nameof(FontSize), typeof(double), typeof(Breadcrumb), 15d); + public static readonly BindableProperty FontSizeProperty = BindableProperty.Create(nameof(FontSize), typeof(double), typeof(Breadcrumb), 15d, BindingMode.OneTime); [TypeConverter(typeof(FontSizeConverter))] public double FontSize @@ -55,7 +55,7 @@ public double FontSize } // Text Color - public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(Breadcrumb), Colors.Black); + public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(Breadcrumb), Colors.Black, BindingMode.OneTime); public Color TextColor { @@ -64,7 +64,7 @@ public Color TextColor } // Corner radius - public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(Breadcrumb), 10f); + public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(Breadcrumb), 10f, BindingMode.OneTime); public float CornerRadius { @@ -73,7 +73,7 @@ public float CornerRadius } // Breadcrumb margin - public static readonly BindableProperty BreadcrumbMarginProperty = BindableProperty.Create(nameof(BreadcrumbMargin), typeof(Thickness), typeof(Breadcrumb), new Thickness(0)); + public static readonly BindableProperty BreadcrumbMarginProperty = BindableProperty.Create(nameof(BreadcrumbMargin), typeof(Thickness), typeof(Breadcrumb), new Thickness(0), BindingMode.OneTime); public Thickness BreadcrumbMargin { @@ -82,7 +82,7 @@ public Thickness BreadcrumbMargin } // BreadcrumbBackgroundColor - public static readonly BindableProperty BreadcrumbBackgroundColorProperty = BindableProperty.Create(nameof(BreadcrumbBackgroundColor), typeof(Color), typeof(Breadcrumb), Colors.Transparent); + public static readonly BindableProperty BreadcrumbBackgroundColorProperty = BindableProperty.Create(nameof(BreadcrumbBackgroundColor), typeof(Color), typeof(Breadcrumb), Colors.Transparent, BindingMode.OneTime); public Color BreadcrumbBackgroundColor { @@ -91,7 +91,7 @@ public Color BreadcrumbBackgroundColor } // LastBreadcrumbTextColor - public static readonly BindableProperty LastBreadcrumbTextColorProperty = BindableProperty.Create(nameof(LastBreadcrumbTextColor), typeof(Color), typeof(Breadcrumb), Colors.Black); + public static readonly BindableProperty LastBreadcrumbTextColorProperty = BindableProperty.Create(nameof(LastBreadcrumbTextColor), typeof(Color), typeof(Breadcrumb), Colors.Black, BindingMode.OneTime); public Color LastBreadcrumbTextColor { @@ -100,7 +100,7 @@ public Color LastBreadcrumbTextColor } // LastBreadcrumbCornerRadius - public static readonly BindableProperty LastBreadcrumbCornerRadiusProperty = BindableProperty.Create(nameof(LastBreadcrumbCornerRadius), typeof(float), typeof(Breadcrumb), 10f); + public static readonly BindableProperty LastBreadcrumbCornerRadiusProperty = BindableProperty.Create(nameof(LastBreadcrumbCornerRadius), typeof(float), typeof(Breadcrumb), 10f, BindingMode.OneTime); public float LastBreadcrumbCornerRadius { @@ -109,7 +109,7 @@ public float LastBreadcrumbCornerRadius } // LastBreadcrumbBackgroundColor - public static readonly BindableProperty LastBreadcrumbBackgroundColorProperty = BindableProperty.Create(nameof(LastBreadcrumbBackgroundColor), typeof(Color), typeof(Breadcrumb), Colors.Transparent); + public static readonly BindableProperty LastBreadcrumbBackgroundColorProperty = BindableProperty.Create(nameof(LastBreadcrumbBackgroundColor), typeof(Color), typeof(Breadcrumb), Colors.Transparent, BindingMode.OneTime); public Color LastBreadcrumbBackgroundColor { @@ -118,7 +118,7 @@ public Color LastBreadcrumbBackgroundColor } // AnimationSpeed - public static readonly BindableProperty AnimationSpeedProperty = BindableProperty.Create(nameof(AnimationSpeed), typeof(uint), typeof(Breadcrumb), (uint)800); + public static readonly BindableProperty AnimationSpeedProperty = BindableProperty.Create(nameof(AnimationSpeed), typeof(uint), typeof(Breadcrumb), (uint)800, BindingMode.OneTime); public uint AnimationSpeed { @@ -127,7 +127,7 @@ public uint AnimationSpeed } // IsNavigationEnabled - public static readonly BindableProperty IsNavigationEnabledProperty = BindableProperty.Create(nameof(IsNavigationEnabled), typeof(bool), typeof(Breadcrumb), true); + public static readonly BindableProperty IsNavigationEnabledProperty = BindableProperty.Create(nameof(IsNavigationEnabled), typeof(bool), typeof(Breadcrumb), true, BindingMode.OneTime); public bool IsNavigationEnabled { @@ -142,7 +142,7 @@ public Breadcrumb() InitializeComponent(); } - async void BreadCrumbContainer_Loaded(object sender, EventArgs e) + async void BreadCrumbContainer_Loaded(object? sender, EventArgs e) { BreadCrumbContainer.Loaded -= BreadCrumbContainer_Loaded; @@ -208,9 +208,6 @@ async void BreadCrumbContainer_Loaded(object sender, EventArgs e) /// /// Creates a new Breadcrumb object /// - /// - /// - /// Border BreadcrumbCreator(Page page, bool isLast, bool isFirst) { // Create border control for the breadcrumb @@ -222,13 +219,12 @@ Border BreadcrumbCreator(Page page, bool isLast, bool isFirst) } : new Border(); - container.BackgroundColor = Colors.Transparent; container.StrokeShape = new RoundRectangle { CornerRadius = isLast ? LastBreadcrumbCornerRadius : CornerRadius }; container.Padding = 10; - container.Stroke = Colors.Transparent; + container.StrokeThickness = 0; container.Margin = BreadcrumbMargin; container.VerticalOptions = LayoutOptions.Center; container.SetBinding(BackgroundColorProperty, new Binding(isLast ? nameof(LastBreadcrumbBackgroundColor) : nameof(BreadcrumbBackgroundColor), source: new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor, typeof(Breadcrumb)))); @@ -239,8 +235,7 @@ Border BreadcrumbCreator(Page page, bool isLast, bool isFirst) { container.Content = new Image { - Source = FirstBreadcrumb, - VerticalOptions = LayoutOptions.Center + Source = FirstBreadcrumb }; } else @@ -248,9 +243,7 @@ Border BreadcrumbCreator(Page page, bool isLast, bool isFirst) Label breadcrumbText = new() { Text = page.Title, - FontSize = FontSize, - VerticalOptions = LayoutOptions.Center, - VerticalTextAlignment = TextAlignment.Center + FontSize = FontSize }; breadcrumbText.SetBinding(Label.TextColorProperty, new Binding(isLast ? nameof(LastBreadcrumbTextColor) : nameof(TextColor), source: new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor, typeof(Breadcrumb)))); AutomationProperties.SetIsInAccessibleTree(breadcrumbText, false); @@ -264,8 +257,6 @@ Border BreadcrumbCreator(Page page, bool isLast, bool isFirst) /// /// Animates item added to stack /// - /// - /// async void AnimatedStack_ChildAdded(object? sender, ElementEventArgs e) { // iOS scroll to end fix @@ -276,10 +267,10 @@ async void AnimatedStack_ChildAdded(object? sender, ElementEventArgs e) Animation lastBreadcrumbAnimation = new() { - { 0, 1, new Animation(_ => ((View)BreadCrumbContainer.Children.Last()).TranslationX = _, Application.Current?.MainPage?.Width ?? 0, 0, Easing.Linear) } + { 0, 1, new Animation(_ => ((View)BreadCrumbContainer.Children[^1]).TranslationX = _, Application.Current?.MainPage?.Width ?? 0, 0, Easing.Linear) } }; - Point point = BreadCrumbsScrollView.GetScrollPositionForElement((View)BreadCrumbContainer.Children.Last(), ScrollToPosition.End); + Point point = BreadCrumbsScrollView.GetScrollPositionForElement((View)BreadCrumbContainer.Children[^1], ScrollToPosition.End); lastBreadcrumbAnimation.Add(0, 1, new Animation(_ => BreadCrumbsScrollView.ScrollToAsync((View?)BreadCrumbContainer.Children.LastOrDefault(), ScrollToPosition.MakeVisible, true), BreadCrumbsScrollView.X, point.X - 6)); lastBreadcrumbAnimation.Commit(this, nameof(lastBreadcrumbAnimation), 16, AnimationSpeed); @@ -288,7 +279,6 @@ async void AnimatedStack_ChildAdded(object? sender, ElementEventArgs e) /// /// Navigates the user back to chosen selectedPage in the Navigation stack /// - /// async Task GoBack(Page selectedPage) { // Check if selectedPage is still in Navigation Stack