c# - Xamarin.Forms FontAwesome 不适用于绑定(bind)属性

标签 c# font-awesome-5 xamarin.forms.listview

我想为我的 Xamarin.Forms 项目添加很棒的字体,我已将其添加到项目中。 然后我将 FontFamily 添加到 Label 中,如下所示:

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

这段代码工作正常,结果是 enter image description here

但是当我将这些硬编码的东西更改为这样的可绑定(bind)属性时:

<Label Grid.Column="0" Text="{Binding FontAwesomeIconText}" FontSize="20">

在c#中这样设置

menuItems = new List<DrawerMenuItem>
            {
                new DrawerMenuItem {Id = MenuItemType.Browse, Title="Browse", FontAwesomeIconText = "&#xf11a;" },
                new DrawerMenuItem {Id = MenuItemType.About, Title="About" , FontAwesomeIconText = "&#xf11a;"}
            };

它坏了。我看到了它的代码

enter image description here

我还尝试在我的 App.xaml 文件中使用 ResourcesDictionary

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

得到了同样的结果。 可能是什么问题?

最佳答案

我在这里找到了解决方案 https://forums.xamarin.com/discussion/30298/fontfamily-not-working-when-using-textproperty-binding

它将像 这样的字符串更改为像 \uf11a 这样的 unicode 值,它对我有用。简单明了

附注祝大家新年快乐! :)

关于c# - Xamarin.Forms FontAwesome 不适用于绑定(bind)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53990920/

相关文章:

c# - 如何解析 Autofac 中的依赖项列表?

css - 使用 Font Awesome 图层在圆圈内渐变

c# - ListView 项目上的 Xamarin Forms AutomationId

c# - 检查 bool 值是否为真?

c# - 在调查问卷样式 DataGridView 中仅从多个复选框列中选择一个复选框

javascript - 在绑定(bind)之前等待 Font Awesome JS 将 i 转换为 svg

xamarin.forms - 如何在 Xamarin Forms 中展开和折叠 ListView

Xamarin 表单从 View 模型绑定(bind)命令到 ListView 数据模板中的按钮

c# - 如何查看一次垃圾回收的内容?

html - 我怎样才能让评级从 fontawesome 5 svg 开始?