【问题标题】:Why can't I get the custom font from the resources?为什么我不能从资源中获取自定义字体?
【发布时间】:2012-07-02 22:57:20
【问题描述】:

我有一个库,其中包含一个 ThemeResources 文件,其中包含样式。

在字体文件夹中,我有一个字体文件 BuxtonSketch.ttf。 Build Action 是 RESOURCE

在 ThemeResources 中,我已经定义了字体:

<FontFamily x:Key="FontFamily-Sketch">pack://application:,,,/Resources/Fonts/#Buxton Sketch</FontFamily>

<Style x:Key="TextNormalStyle" TargetType="TextBlock">
    <Setter Property="FontFamily" Value="{StaticResource FontFamily-Sketch}" />
</Style>

由于我的 UserControl 调用了 ProblemUserControl,我调用了但没有工作

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Resources/ThemeResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

<Grid>
    <TextBlock Text="La pelicula PROMETHEUS!" Style="{StaticResource TextNormalStyle}" />
</Grid>

会发生什么?

【问题讨论】:

  • TextNormalStyle 是在哪里定义的?
  • @ShawnKendrot Ups,我忘记了,但我已经添加了。对不起!

标签: c# wpf silverlight xaml resources


【解决方案1】:

This blogthis blog 都使用以下语法来使用自定义字体

博客 1:

<TextBlock Text="SOTC Custom Fonts!" FontFamily="VINERITC.TTF#Viner Hand ITC"/>

博客2:

<TextBlock Text="Gothic" FontFamily="./GOTHIC.TTF#Century Gothic"/>

所以对你来说,我认为它会是

<TextBlock Text="Buxton" FontFamily="BuxtonSketch.TTF#Buxton Sketch"/>

或为你的风格

<Style x:Key="TextNormalStyle" TargetType="TextBlock">
    <Setter Property="FontFamily" Value="BuxtonSketch.TTF#Buxton Sketch" />
</Style>

您可能需要包含文件夹位置,但不确定

<TextBlock Text="Buxton" FontFamily="Resources/Fonts/BuxtonSketch.TTF#Buxton Sketch"/>

【讨论】:

  • 我已经为这个问题苦苦挣扎了很多次。感谢您的回答,我刚刚发现关键是在井号之前添加文件名
【解决方案2】:

您还可以将字体添加到 .zip 文件并像这样访问:

<Setter Property="FontFamily" Value="../Resources/Fonts/Fonts.zip#BuxtonSketch"/>

【讨论】:

    【解决方案3】:

    我刚刚花了一整天的时间研究这个问题。将字体复制到项目中时,请确保 Visual Studio 将这些项目的 Build Action 设置为 Resource。我所要做的就是在下拉列表中更改它。

    【讨论】:

      猜你喜欢
      • 2015-02-07
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多