如何在用户控件库中定义ResourceDictionary并通过Xaml-Code访问它们 .
我创造了这样的东西:
<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”与Control一起使用
Style="{StaticResource NormalStyle}"
但是Visual Studio说“资源”NormalStyle“无法解决” . 我想念或忘记了什么吗?
谢谢你的帮助
3 回答
您必须包含或合并
ResourceDictionary
与UserControl.Resources
,如下所示 . 这里在Source中给出了ResourceDictionary
的路径 .要么
然后,您可以使用UserControl内的字典中的资源
请在App.xamal中添加样式引用,以便在运行应用程序时加载它 . 当您添加任何外部资源文件时,您需要在App.xamal中添加它以在您的应用程序中获取它的引用,否则您需要手动添加对每个xamal表单的引用以访问样式和模板
这就是你要使用MergedDictionaries在App.xamal中添加它的方式,而不是你在应用程序中引用任何样式的地方,它将有它的参考
希望有所帮助
好吧,所以我得到了一半的工作 . 在我的UserControlLibrary中,我创建了一个新的Xaml-File“UIStandards”,其中放入了我的所有样式 . 现在在我的主项目中,我想访问这些样式,我想我应该使用MergedDictionary .
我做的是这样的:
正如您所看到的,我引用了UISTandards.xaml,但是在MainProject内的UserControl中,我找不到任何样式 . 有解决方案?