问题
I want to add font awesome to my Xamarin.Forms project and I have added it to the project. Then I have added FontFamily to a Label like this:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="15">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="9*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="" FontSize="20">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="Font Awesome 5 Free" />
<On Platform="Android" Value="fa-regular-400.ttf#Font Awesome 5 Free Regular" />
</OnPlatform>
</Label.FontFamily>
</Label>
<Label Grid.Column="1" Text="{Binding Title}" FontSize="20"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
This code works fine and this is the result
But when I change this hardcoded stuff to a bindable property like this:
<Label Grid.Column="0" Text="{Binding FontAwesomeIconText}" FontSize="20">
And set it like this in c#
menuItems = new List<DrawerMenuItem>
{
new DrawerMenuItem {Id = MenuItemType.Browse, Title="Browse", FontAwesomeIconText = "" },
new DrawerMenuItem {Id = MenuItemType.About, Title="About" , FontAwesomeIconText = ""}
};
It breaks. And I see its code
I also tried to use a ResourcesDictionary in my App.xaml file
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesome">
<On Platform="iOS" Value="Font Awesome 5 Free" />
<On Platform="Android" Value="fa-regular-400.ttf#Font Awesome 5 Free Regular" />
</OnPlatform>
And got the same result. What might be the problem?
回答1:
I found the solution here https://forums.xamarin.com/discussion/30298/fontfamily-not-working-when-using-textproperty-binding
It's changing the string like 
to a unicode value like \uf11a
And it works for me. Simple and clear
p.s. Happy New Year to everyone! :)
来源:https://stackoverflow.com/questions/53990920/xamarin-forms-fontawesome-doesnt-work-with-bound-properties