From dd59107a4b698f2cccaef343266eecfabb3afebc Mon Sep 17 00:00:00 2001 From: Mohamed CHOUCHANE Date: Thu, 13 Sep 2018 17:32:11 +0200 Subject: [PATCH] Fix : Binding Icon in ToolbarItem in TabbedPage doesn't refresh (#3761) fixes #3756 --- .../Converters/IconConveter.cs | 46 +++++++++++++++++++ .../Renderers/VisualPageRenderer.cs | 45 ++++-------------- .../Xamarin.Forms.Platform.WPF.csproj | 1 + 3 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 Xamarin.Forms.Platform.WPF/Converters/IconConveter.cs diff --git a/Xamarin.Forms.Platform.WPF/Converters/IconConveter.cs b/Xamarin.Forms.Platform.WPF/Converters/IconConveter.cs new file mode 100644 index 000000000..a2fe97205 --- /dev/null +++ b/Xamarin.Forms.Platform.WPF/Converters/IconConveter.cs @@ -0,0 +1,46 @@ +using System; +using System.Globalization; +using System.IO; +using System.Windows.Media; +using Xamarin.Forms.Platform.WPF.Controls; +using Xamarin.Forms.Platform.WPF.Enums; + +namespace Xamarin.Forms.Platform.WPF.Converters +{ + public class IconConveter : System.Windows.Data.IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is FileImageSource imageSource) + { + if (Enum.TryParse(imageSource.File, true, out Symbol symbol)) + return new FormsSymbolIcon() { Symbol = symbol }; + else if (TryParseGeometry(imageSource.File, out Geometry geometry)) + return new FormsPathIcon() { Data = geometry }; + else if (Path.GetExtension(imageSource.File) != null) + return new FormsBitmapIcon() { UriSource = new Uri(imageSource.File, UriKind.RelativeOrAbsolute) }; + } + + return null; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + + private bool TryParseGeometry(string value, out Geometry geometry) + { + geometry = null; + try + { + geometry = Geometry.Parse(value); + return true; + } + catch (Exception) + { + return false; + } + } + } +} diff --git a/Xamarin.Forms.Platform.WPF/Renderers/VisualPageRenderer.cs b/Xamarin.Forms.Platform.WPF/Renderers/VisualPageRenderer.cs index 5382c13de..50f40c338 100644 --- a/Xamarin.Forms.Platform.WPF/Renderers/VisualPageRenderer.cs +++ b/Xamarin.Forms.Platform.WPF/Renderers/VisualPageRenderer.cs @@ -1,12 +1,10 @@ using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; -using System.IO; using System.Windows.Media; using System.Windows.Media.Imaging; using Xamarin.Forms.Platform.WPF.Controls; -using Xamarin.Forms.Platform.WPF.Enums; +using Xamarin.Forms.Platform.WPF.Converters; using Xamarin.Forms.Platform.WPF.Extensions; namespace Xamarin.Forms.Platform.WPF @@ -52,7 +50,6 @@ namespace Xamarin.Forms.Platform.WPF UpdateBackButtonTitle(); else if (e.PropertyName == NavigationPage.HasNavigationBarProperty.PropertyName) UpdateNavigationBarVisible(); - } void UpdateTitle() @@ -100,32 +97,22 @@ namespace Xamarin.Forms.Platform.WPF foreach (var item in Element.ToolbarItems) { - FormsAppBarButton appBar = new FormsAppBarButton() + var appBar = new FormsAppBarButton() { DataContext = item }; + + var iconBinding = new System.Windows.Data.Binding(nameof(item.Icon)) { - Label = item.Text, - Tag = item + Converter = new IconConveter() }; + appBar.SetBinding(FormsAppBarButton.IconProperty, iconBinding); + appBar.SetBinding(FormsAppBarButton.LabelProperty, nameof(item.Text)); appBar.SetValue(FrameworkElementAttached.PriorityProperty, item.Priority); - if(item.Icon != null) - { - Symbol symbol; - Geometry geometry; - - if (Enum.TryParse(item.Icon.File, true, out symbol)) - appBar.Icon = new FormsSymbolIcon() { Symbol = symbol }; - else if (TryParseGeometry(item.Icon.File, out geometry)) - appBar.Icon = new FormsPathIcon() { Data = geometry }; - else if (Path.GetExtension(item.Icon.File) != null) - appBar.Icon = new FormsBitmapIcon() { UriSource = new Uri(item.Icon.File, UriKind.RelativeOrAbsolute) }; - } - appBar.Click += (sender, e) => { - if (appBar.Tag != null && appBar.Tag is ToolbarItem) + if (appBar.DataContext is ToolbarItem toolbarItem) { - (appBar.Tag as ToolbarItem).Activate(); + toolbarItem.Activate(); } }; @@ -153,20 +140,6 @@ namespace Xamarin.Forms.Platform.WPF UpdateToolbar(); } - private bool TryParseGeometry(string value, out Geometry geometry) - { - geometry = null; - try - { - geometry = Geometry.Parse(value); - return true; - } - catch(Exception) - { - return false; - } - } - bool _isDisposed; protected override void Dispose(bool disposing) diff --git a/Xamarin.Forms.Platform.WPF/Xamarin.Forms.Platform.WPF.csproj b/Xamarin.Forms.Platform.WPF/Xamarin.Forms.Platform.WPF.csproj index a21688770..87077ec03 100644 --- a/Xamarin.Forms.Platform.WPF/Xamarin.Forms.Platform.WPF.csproj +++ b/Xamarin.Forms.Platform.WPF/Xamarin.Forms.Platform.WPF.csproj @@ -87,6 +87,7 @@ +