不从WPF中的资源字典加载包含的字体



在我的WPF应用程序中,我在App.xaml文件中包含了我正在使用的字体,如下所示:

<Application x:Class="STFAPP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:STFAPP"
StartupUri="Views/LoginWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<!--Included Fonts-->
<FontFamily x:Key="TheSans">pack://application:,,,/Style/Fonts/#TheSans</FontFamily>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/TextBlock.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

TextBlock.xaml是包括TextBlock样式的资源字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type TextBlock}" x:Key="TitleStyle">
<Setter Property="FontFamily" Value="{StaticResource TheSans}" />
<Setter Property="FontSize" Value="15" />
</Style>

我在TextBlock中使用了以下样式:

<TextBlock Text="Hello" Style="{StaticResource TitleStyle}"/>

字体未加载!?

如果我避免使用分离的资源字典(TextBlock.xaml文件(,并将样式直接粘贴到App.xaml,字体将成功加载:

<Application x:Class="STFAPP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:STFAPP"
StartupUri="Views/LoginWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<!--Included Fonts-->
<FontFamily x:Key="TheSans">pack://application:,,,/Style/Fonts/#TheSans</FontFamily>
<Style TargetType="{x:Type TextBlock}" x:Key="TitleStyle">
<Setter Property="FontFamily" Value="{StaticResource TheSans}" />
<Setter Property="FontSize" Value="15" />
</Style>
</ResourceDictionary>
</Application.Resources>

我想在多个资源字典文件中分离样式,以组织我的应用程序文件。

我将感谢的任何帮助

Style:中更改为DynamicResource

<Setter Property="FontFamily" Value="{DynamicResource TheSans}" />

或者将FontFamily资源移动到TextBlock.xaml或另一个资源字典,例如Fonts.xaml,并合并这个:

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/Fonts.xaml" />
<ResourceDictionary Source="Style/TextBlock.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

最新更新