Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix : Binding Icon in ToolbarItem in TabbedPage doesn't refresh (#3761)
Browse files Browse the repository at this point in the history
fixes #3756
  • Loading branch information
mohachouch authored and rmarinho committed Sep 13, 2018
1 parent cceee9d commit dd59107
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
46 changes: 46 additions & 0 deletions Xamarin.Forms.Platform.WPF/Converters/IconConveter.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
45 changes: 9 additions & 36 deletions Xamarin.Forms.Platform.WPF/Renderers/VisualPageRenderer.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -52,7 +50,6 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdateBackButtonTitle();
else if (e.PropertyName == NavigationPage.HasNavigationBarProperty.PropertyName)
UpdateNavigationBarVisible();

}

void UpdateTitle()
Expand Down Expand Up @@ -100,32 +97,22 @@ void UpdateToolbar()

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();
}
};

Expand Down Expand Up @@ -153,20 +140,6 @@ private void VisualPageRenderer_CollectionChanged(object sender, System.Collecti
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<Compile Include="Converters\CollapseWhenEmptyConverter.cs" />
<Compile Include="Converters\ColorConverter.cs" />
<Compile Include="Converters\HeightConverter.cs" />
<Compile Include="Converters\IconConveter.cs" />
<Compile Include="Converters\ImageConverter.cs" />
<Compile Include="Converters\SymbolToValueConverter.cs" />
<Compile Include="Converters\ToUpperConverter.cs" />
Expand Down

0 comments on commit dd59107

Please sign in to comment.