c# - 无法在运行时显示自定义字体 - Windows Phone 8.1 MVVM - FontAwesome

标签 c# mvvm fonts windows-phone-8.1 font-awesome

我在使用 Windows Phone 8.1 MVVM 应用程序中的自定义字体时遇到问题。

我正在使用 FontAwesome 图标。我在我的项目中包含了 FontAwesome 字体文件。当我设置这样的静态控件时,它工作得很好;

<TextBlock x:Name="txtTest" Grid.Row="3" Text="&#xf236;" Foreground="Black" FontSize="20" FontFamily="/Assets/Fonts/FontAwesome.ttf#FontAwesome"/>

但是,我需要的是让它动态工作。我在应用程序的主页上有一个 Hub 控件,每个 Hub 部分都有 ListViews。它们绑定(bind)到自定义对象的集合,从 API 响应中填充。创建对象集合时,代码会在响应中查找标记并根据标记动态设置 FontAwesome 图标。

中心部分代码:

<HubSection x:Uid="hubApproved" Header="Approved"
                    DataContext="{Binding MyObjects.Approved}" 
                    d:DataContext="{Binding MyObjects.Approved}"
                    HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}" >
            <DataTemplate>
                <ListView
                    ItemsSource="{Binding}"                                                   
                    ItemTemplate="{ThemeResource ApprovedTemplate}"                        
                    IsItemClickEnabled="True"
                    ItemClick="ListView_ItemClick"
                    ContinuumNavigationTransitionInfo.ExitElementContainer="True">
                </ListView>
            </DataTemplate>
        </HubSection>

这是绑定(bind)到此的批准模板:

<DataTemplate x:Key="ApprovedTemplate">
    <StackPanel Margin="0,0,0,19" Background="{x:Null}" >
        <TextBlock FontFamily="/Assets/Fonts/FontAwesome.ttf#FontAwesome" Text="{Binding Icon}" Foreground="Black" />
        <TextBlock Text="{Binding SupplierName}" Style="{ThemeResource ListViewItemTripNameTextBlockStyle}" />            
        <TextBlock Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}" Text="{Binding StartDate}"></TextBlock>
    </StackPanel>
</DataTemplate>

模板包含绑定(bind)到我的对象的 Icon 属性的 TextBlock。这应该会显示适当的 FontAwesome 图标,但实际上只显示图标的 unicode:

enter image description here

我尝试从 View 后面的代码中定义 Hub 控件的字体系列,但没有效果:

Hub.FontFamily = new FontFamily("ms-appx:///Assets/Fonts/FontAwesome.otf#FontAwesome");

关于如何动态显示这些图标的任何想法...?谢谢

最佳答案

你应该可以这样做:

FontFamily fontFam = new FontFamily("ms-appx:///Assets/Fonts/FontAwesome.otf#FontAwesome");

然后像这样设置 FontFamily:

Hub.FontFamily = fontFam

关于c# - 无法在运行时显示自定义字体 - Windows Phone 8.1 MVVM - FontAwesome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37986873/

相关文章:

c# - 在windows sdk中没有找到引用(Windows 1903)

c# - 如果返回的行为 NULL,则将 List<T> 项设置为空

c# - 如何更改 TabbedPage 中的默认选定项?

css - 在 tumblr 中使用 @font-face 不使用 RTL 脚本

ios - 无法在 iOS 中使用 Maestro 字体

c# - Silverlight 与 MVVM : How to access Event of the ViewModel from the View?

c# - 嵌套类——这不是一个不完整的类型吗

.net - 加载 View 模型在缓存 MVVM 灯中找不到 IDataErrorInfo 类型

wpf - WPF如何在MVVM中执行从主ViewModel到子ViewModel的命令

html - 字体在旧版浏览器中显示为 Times New Roman