Xamarin.Forms FontAwesome doesn't work with bound properties

╄→гoц情女王★ 提交于 2020-07-07 11:06:33

问题


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="&#xf11a;" 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 = "&#xf11a;" },
                new DrawerMenuItem {Id = MenuItemType.About, Title="About" , FontAwesomeIconText = "&#xf11a;"}
            };

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 &#xf11a; 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!