用户控制库中的 xaml ResourceDictionary [英] xaml ResourceDictionary inside User Control Library
问题描述
如何在用户控件库中定义 ResourceDictionary 并通过 Xaml-Code 访问它们.
how is it possible to define a ResourceDictionary inside a User Control Library and access them via Xaml-Code.
我已经创建了这样的东西:
I've created something like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
>
<Style x:Key="NormalStyle" TargetType="{x:Type Control}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontSize" Value="12" />
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="FontStyle" Value="Normal" />
</Style>
.
.
.
</ResourceDictionary
现在我想在控件中使用这个NormalStyle"
And now I want to use this "NormalStyle" with a Control
Style="{StaticResource NormalStyle}"
但 Visual Studio 显示无法解析资源NormalStyle"."我是否错过或忘记了什么?
But Visual Studio says "The resource "NormalStyle" could not be resolved." Did i miss or forget something?
谢谢你的帮助
推荐答案
您将必须包含或合并您的 ResourceDictionary
和 UserControl.Resources
,如下所示.在 Source 中,请提供您的 ResourceDictionary
的路径.
You will have to include or merge your ResourceDictionary
with UserControl.Resources
like below. Here in Source give path to your ResourceDictionary
.
<UserControl.Resources>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</UserControl.Resources>
或
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
然后你就可以在你的 UserControl 里面使用字典中的资源了
Then you can use the resources in the dictionary inside your UserControl
Style="{StaticResource NormalStyle}"
这篇关于用户控制库中的 xaml ResourceDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!