maui: Unable to bind `Glyph` property in `FontImageSource` of `ToolbarItem`

Description

Original discussion: https://forums.xamarin.com/discussion/180852/why-glyph-property-in-fontimagesource-doesnt-bind Xamarin issue: https://github.com/xamarin/Xamarin.Forms/issues/12700

This code doesn’t produce any errors, but Glyph is always empty:

<ContentPage.ToolbarItems>
    <ToolbarItem IconImageSource="{FontImageSource Glyph={Binding MyIcon}, FontFamily=MaterialDesignIcons}" />
</ContentPage.ToolbarItems>

Static binding works fine. Code above also works if it’s added via Hot Reload (but after proper app reload icon is gone again)

image

Steps to Reproduce

  1. Add ToolbarItem to your page
  2. SpecifyIconImageSource to it as FontImageSource with binded Glyph

Link to public reproduction project repository

Remove the converter and start the app: https://github.com/maxkoshevoi/NureTimetable/blob/5f67cadbb78bc3c37df4985adf8c4ce3a936b4a0/NureTimetable/UI/Views/Timetable/TimetablePage.xaml#L20

Version with bug

6.0.486 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

All (tested on 13)

Did you find any workaround?

Thanks Saskia.

<ToolbarItem IconImageSource="{Binding MyIcon, Converter={converters:ToolbarIconValueConverter}}" />
public class ToolbarIconValueConverter : IValueConverter, IMarkupExtension
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return new FontImageSource
        {
            FontFamily = (OnPlatform<string>)Application.Current.Resources["MaterialFontFamily"],
            Glyph = (string)value
        };
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

Relevant log output

No response

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 2
  • Comments: 25 (10 by maintainers)

Most upvoted comments

The problem was, that you have to escape Unicode codes differently in csharp and XAML.

XAML : “&#xf308;” is equivalent to C# “\uf308”

Changing the escape format in the ValueConverter to the correct C# format yields correct results…

https://www.reddit.com/r/xamarindevelopers/comments/c0jnt5/fontimagesource_glyph_icon_with_data_binding_only/

Following my previous post, an obvious dirty workaround that’s workings using code in a ViewModel. The main principle is to change ToolbarItems[0] item with a ToolbarItem depending on a property.

public IList<ToolbarItem> ToolbarItems;   // Set it at startup in the page code behind
private ToolbarItem _toolbarItemGeoLocationIconStatusOFF;
private ToolbarItem _toolbarItemGeoLocationIconStatusON;

// Create dynamically toolbar Item Geo Location Icon Status ON
// Woraround of Binding that's not working
FontImageSource fontImageSourceGeoLocationIconStatusON = new FontImageSource()
{
	FontFamily = "MaterialIconsOutlined-Regular",
	Glyph = "\ue0c8",
	FontAutoScalingEnabled = true,
	Color = Colors.Black
};
_toolbarItemGeoLocationIconStatusON = new ToolbarItem
{
	Text = "",
	IconImageSource = fontImageSourceGeoLocationIconStatusON
};


// Create dynamically toolbar Item Geo Location Icon Status OFF
// Woraround of Binding that's not working
FontImageSource fontImageSourceGeoLocationIconStatusOFF = new FontImageSource()
{
	FontFamily = "MaterialIconsOutlined-Regular",
	Glyph = "\ue0c7",
	FontAutoScalingEnabled = true,
	Color = Colors.Black
};
_toolbarItemGeoLocationIconStatusOFF = new ToolbarItem
{
	Text = "",
	IconImageSource = fontImageSourceGeoLocationIconStatusOFF
};

...

if (ToolbarItems.Count == 0)
{
	// Set Geo Location Indicator default Status
	ToolbarItems.Add(_toolbarItemGeoLocationIconStatusOFF);
}

if (IsGeoLocationAcquired)
{
	ToolbarItems[0] = _toolbarItemGeoLocationIconStatusON;
}
else
{
	ToolbarItems[0] = _toolbarItemGeoLocationIconStatusOFF;
}

Sorry I tested it with regular image not with toolbar, but it’s not working with toolbar I’m sorry for that

Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows

From: Maksym @.> Sent: Wednesday, November 9, 2022 10:32 AM To: @.> Cc: Michael @.>; @.> Subject: Re: [dotnet/maui] Unable to bind Glyph property in FontImageSource of ToolbarItem (Issue #10186)

Yes it’s working without converter

Could you please share a sample project? It doesn’t work on my side as I’ve described here: #10186 (comment)https://github.com/dotnet/maui/issues/10186#issuecomment-1268835353

— Reply to this email directly, view it on GitHubhttps://github.com/dotnet/maui/issues/10186#issuecomment-1308391797, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADRJJRWYWAM3APJ7D2AHCS3WHNOTRANCNFSM6AAAAAAQPE72UI. You are receiving this because you were mentioned.Message ID: @.***>

My bad, you were talking about dynamic binding for glyph. Sorry