Fix : Binding Icon in ToolbarItem in TabbedPage doesn't refresh (#3761) fixes #3756

This commit is contained in:
Mohamed CHOUCHANE 2018-09-13 17:32:11 +02:00 коммит произвёл Rui Marinho
Родитель cceee9ded6
Коммит dd59107a4b
3 изменённых файлов: 56 добавлений и 36 удалений

Просмотреть файл

@ -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;
}
}
}
}

Просмотреть файл

@ -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)

Просмотреть файл

@ -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" />