[英]Why can't I use my static resource in a resource dictionary in wpf?
I designed my control (radio button) using a ResourceDictionary
like this:我使用这样的
ResourceDictionary
设计了我的控件(单选按钮):
//in SidebarMenuButtonTheme.xaml
<Style TargetType="{x:Type custom:MenuItem}"
x:Key="MenuButtonTheme">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type custom:MenuItem}">
<Grid Background="{TemplateBinding Background}">
<TextBlock x:Name="SidebarRadioButtonMenuText"
Text="{TemplateBinding Property=Content}"
Foreground="#7D8083"
FontSize="14.59"
FontFamily="{StaticResource NunitoBold}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The FontFamily
works just fine when I define the Source
: eg当我定义
Source
时, FontFamily
工作得很好:例如
FontFamily="/UiDesign/Fonts/#Nunito"
But when I use StaticResource
for the FontFamily
但是当我为
FontFamily
使用StaticResource
FontFamily="{StaticResource NunitoBold}"
I receive this error in my UI:我在 UI 中收到此错误:
(The error is: The element [MenuItem] of type MenuItem could not be displayed) (错误是:无法显示MenuItem类型的元素[MenuItem])
This is how I declare my font resource in my App.xaml这就是我在 App.xaml 中声明字体资源的方式
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/UiDesign/Theme/SidebarMenuButtonTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
<FontFamily x:Key="NunitoBold">/UiDesign/Fonts/Nunito-Bold.ttf#Nunito</FontFamily>
</ResourceDictionary>
</Application.Resources>
I don't know why this is giving an error, even though intellisense works when I type StaticResource
:我不知道为什么这会出错,即使智能感知在我输入
StaticResource
时有效:
But when I define my FontFamily
resource in the ResourceDictionary
, the menu items will display just fine.但是当我在
ResourceDictionary
中定义我的FontFamily
资源时,菜单项会显示得很好。
//I added this above my Style tag...
<FontFamily x:Key="NunitoBold">/UiDesign/Fonts/Nunito-Bold.ttf#Nunito</FontFamily>
Update: But, when I use the StaticResource
(for the font) in a UserControl
, I can use it just fine.更新:但是,当我在
UserControl
中使用StaticResource
(用于字体)时,我可以很好地使用它。 My problem here is that, can I use the resource in App.xaml, in a ResourceDictionay?我的问题是,我可以在 ResourceDictionay 中使用 App.xaml 中的资源吗?
Try to reference the font using a pack URI :尝试使用包 URI引用字体:
<FontFamily x:Key="NunitoBold">pack://application:,,,/UiDesign/Fonts/Nunito-Bold.ttf#Nunito</FontFamily>
You also need to ensure that the paths is correct, ie that UiDesign
is a folder at the root of your project in which the Fonts
folder with the font is located.您还需要确保路径正确,即
UiDesign
是项目根目录下的文件夹,其中包含字体的Fonts
文件夹所在的位置。
I already found a solution for this, instead of using StaticResource
I used DynamicResource
instead.我已经找到了解决方案,我没有使用
StaticResource
,而是使用了DynamicResource
。 Now I can access my fonts from App.xaml
to my ResourceDictionary
.现在我可以从
App.xaml
到我的ResourceDictionary
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.