Xamarin MVVM 自定义字体在数据模板中不起作用

Xamarin MVVM Custom Font Not Working In Data Template

使用字体时遇到问题。当我执行以下操作时工作正常。

FontFamily="{StaticResource IconFonts}" Text=""

但是如果我尝试动态绑定文本,它不起作用 属性。

FontFamily="{StaticResource IconFonts}" Text="{Binding Comments}"

它看起来很奇怪。看起来它试图做某事但没有完全呈现。这是完整的代码,下面包含我的测试。提前致谢。

<FlexLayout x:Name="FlexFood"
        Wrap="Wrap"
        Direction="Row"
        JustifyContent="Start"
        Padding="0, 10"
        BindableLayout.ItemsSource="{Binding FoodCollection}">
<BindableLayout.ItemTemplate>
    <DataTemplate>
        <StackLayout Padding="5, 0">

                    <Frame.GestureRecognizers>

                        <TapGestureRecognizer Tapped="OnTypesSelected"></TapGestureRecognizer>

                    </Frame.GestureRecognizers>

                    <Frame.Content>
                        <StackLayout x:Name="LayoutFood">

                            <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
                                <Label x:Name="LabelTitle" Padding="0, 10">
                                <Label.FormattedText>
                                    <FormattedString>
                                        <Span FontSize="Subtitle" Text="{Binding Title}" FontAttributes="Bold"></Span>
                                        <Span Text=" "></Span>
                                        <Span FontSize="Subtitle" Text="{Binding Version}" FontAttributes="Bold"></Span>
                                    </FormattedString>
                                </Label.FormattedText>
                            </Label>
                            </StackLayout>

                            <StackLayout Orientation="Horizontal">

                            <StackLayout HorizontalOptions="StartAndExpand">

                                <Label Text="{Binding Description}" FontSize="Body"></Label>-->
                                <Label FontSize="Caption" TextColor="#LELELE">
                                    <Label.FormattedText>
                                        <FormattedString>
                                            <Span Text="{Binding fDepartment}"></Span>
                                            <Span Text="{Binding pType}"></Span>
                                        </FormattedString>
                                    </Label.FormattedText>
                                </Label>

                            </StackLayout>

                            <StackLayout HorizontalOptions="End">

                            <Label Text="{Binding Comments}"></Label>

                            <Label FontFamily="{StaticResource IconFonts}" Text="&#xf139;"></Label>

                            <Label x:Name="LabelTypeTest" FontFamily="{StaticResource IconFonts}"
                                    Text="{Binding Comments, StringFormat='&#xf123;'}"></Label>

                            <Label FontFamily="{StaticResource IconFonts}" Text="{Binding Comments}"></Label>

                            <Image x:Name="ImageTypeTest">
                                <Image.Source>
                                    <FontImageSource Size="48" FontFamily="{StaticResource IconFonts}" Color="#000000"></FontImageSource>
                                </Image.Source>
                            </Image>

                            </StackLayout>

                            </StackLayout>

                        </StackLayout>
                    </Frame.Content>

            </helper:LayoutGradient>
        </StackLayout>
    </DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>

这里是应用中的静态资源

<ResourceDictionary>

    <OnPlatform x:Key="IconFonts" x:TypeArguments="x:String">

        <On Platform="iOS" Value="Material Design Icons"></On>

        <On Platform="Android" Value="IconFonts.ttf#Material Design Icons"></On>

        <On Platform="UWP" Value="/Assets/IconFonts.ttf#Material Design Icons"></On>

    </OnPlatform>

</ResourceDictionary>

您应该 return 来自您的绑定的转义序列 \uf139 而不是 &#xf139;

您可以阅读来源 link 中的 XAML 部分中的从绑定访问和直接访问以获取更多信息。

参考: