我试图将Google字体添加到我的Silverlight项目中。所以我下载了zip并将字体添加到了Fonts文件夹中:
c# - 在Silverlight中添加自定义字体-LMLPHP

我试图这样加载它:



<controls:DynamicTextBlock Grid.Column="2"
    Width="200"
    HorizontalAlignment="Left"
    FontSize="{StaticResource FontSize6}"
    FontFamily="/EZTrader;Component/Fonts/#RobotoLight"
    Foreground="White"
    Text="{Binding UserFullName}"
    ToolTipService.ToolTip="{Binding UserFullName}" />


没事
我应该怎么做才能解决这个问题?

谢谢!

最佳答案

如果使用的是Windows,请使用Windows Font Viewer打开要使用的字体。

检查Font name:。该名称是您引用该名称时要使用的名称。注意:字体名称可能并不总是与.ttf的文件名匹配,并且还可以包含空格。

您希望确保项目中包含文件的“生成操作”设置为“资源”,因为您希望能够从xaml引用它。

您可以在App.xaml中为FontFamily创建静态资源,以便可以在整个项目中引用它。

假设项目的程序集名称为EzTrader.dll



<FontFamily x:Key="RobotoLightFontFamily">/EzTrader;component/Fonts/RobotoLight.ttf#[Font name here]</FontFamily>
<FontFamily x:Key="RobotoThinFontFamily">/EzTrader;component/Fonts/Roboto-Thin.ttf#[Font name here]</FontFamily>
<!-- other font resources -->


然后构建项目。

从那里您应该能够像这样引用它



<controls:DynamicTextBlock Grid.Column="2"
    Width="200"
    HorizontalAlignment="Left"
    FontSize="{StaticResource FontSize6}"
    FontFamily="{StaticResource RobotoLightFontFamily}"
    Foreground="White"
    Text="{Binding UserFullName}"
    ToolTipService.ToolTip="{Binding UserFullName}" />


参考:

How to use your own fonts within Silverlight

Using Custom Fonts in Silverlight

Using built-in, embedded and streamed fonts in Silverlight

10-04 21:37
查看更多