c# - 在 silverlight 中添加自定义字体

标签 c# wpf xaml silverlight fonts

我尝试将谷歌字体添加到我的 silverlight 项目中。所以我下载了 zip 并将字体添加到 Fonts 文件夹中: enter image description here

我尝试像这样加载它:

<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 字体查看器 打开您要使用的字体

检查字体名称:。该名称是您在引用它时将使用的名称。 注意:字体名称可能并不总是与.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

关于c# - 在 silverlight 中添加自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36202457/

相关文章:

c# - 通用项目的 WPF 转换

c# - 如果函数包含同名的局部函数,如何递归调用函数?

wpf - SystemCommands.[SomeWindowAction] 在命令之外 :

wpf - 将选中的菜单项绑定(bind)到 ViewModel 命令

c# - WPF Datagrid - 订阅单元格加载事件?

c# - WPF 绑定(bind) DataGrid 中的 SelectedItem

c# - WPF - 增加现有样式的滚动条宽度

c# - 编辑时禁用用户名验证

c# - asp.net MVC自定义Authorize属性,传递参数和方法细节

C# 将整数显示为二进制/十进制。怎么换显示器?