xaml 用户控制库中的 ResourceDictionary
xaml ResourceDictionary inside User Control Library
如何在用户控制库中定义 ResourceDictionary 并通过 Xaml-代码访问它们。
我创建了这样的东西:
<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" 与控件一起使用
Style="{StaticResource NormalStyle}"
但是 Visual Studio 说“资源 "NormalStyle" 无法解析。”
我错过或忘记了什么吗?
谢谢你帮助我
您必须包含或合并您的 ResourceDictionary
与 UserControl.Resources
,如下所示。在源代码中提供 ResourceDictionary
的路径。
<UserControl.Resources>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</UserControl.Resources>
或
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
然后你就可以在你的UserControl里面使用字典里的资源了
Style="{StaticResource NormalStyle}"
因此请在 App.xamal 中添加样式参考,以便在您 运行 应用程序时加载它。
当您添加任何外部资源文件时,您需要将其添加到 App.xamal 中以在您的应用中获取它的引用,否则您需要手动添加对每个 xamal 表单的引用以访问样式和模板
这就是您使用 MergedDictionaries 在 App.xamal 中添加它的方式,而不是在您的应用程序中引用任何样式的任何地方它都会有它的引用
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/CommonStyles.xaml"/>
<ResourceDictionary Source="Project/Common/CommonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
希望对您有所帮助
好吧,我成功了一半。在我的 UserControlLibrary 中,我创建了一个新的 Xaml-File "UIStandards",将我所有的样式放入其中。现在在我的主项目中,我想访问这些样式,我想我应该使用 MergedDictionary。
我所做的是:
<Application x:Class="MentorPlusClientWindowsWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MentorPlusClientWindowsWPF"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Startup="Application_Startup">
<!--StartupUri="MainWindow.xaml"-->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/CrossProjectUserControls;component/UIStandards.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
如您所见,我引用了 UISTandards.xaml,但在我的 MainProject 中的 UserControl 中,可以找到我的 none 样式。
有什么解决办法吗?
如何在用户控制库中定义 ResourceDictionary 并通过 Xaml-代码访问它们。
我创建了这样的东西:
<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" 与控件一起使用
Style="{StaticResource NormalStyle}"
但是 Visual Studio 说“资源 "NormalStyle" 无法解析。” 我错过或忘记了什么吗?
谢谢你帮助我
您必须包含或合并您的 ResourceDictionary
与 UserControl.Resources
,如下所示。在源代码中提供 ResourceDictionary
的路径。
<UserControl.Resources>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</UserControl.Resources>
或
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
然后你就可以在你的UserControl里面使用字典里的资源了
Style="{StaticResource NormalStyle}"
因此请在 App.xamal 中添加样式参考,以便在您 运行 应用程序时加载它。 当您添加任何外部资源文件时,您需要将其添加到 App.xamal 中以在您的应用中获取它的引用,否则您需要手动添加对每个 xamal 表单的引用以访问样式和模板
这就是您使用 MergedDictionaries 在 App.xamal 中添加它的方式,而不是在您的应用程序中引用任何样式的任何地方它都会有它的引用
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/CommonStyles.xaml"/>
<ResourceDictionary Source="Project/Common/CommonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
希望对您有所帮助
好吧,我成功了一半。在我的 UserControlLibrary 中,我创建了一个新的 Xaml-File "UIStandards",将我所有的样式放入其中。现在在我的主项目中,我想访问这些样式,我想我应该使用 MergedDictionary。
我所做的是:
<Application x:Class="MentorPlusClientWindowsWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MentorPlusClientWindowsWPF"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Startup="Application_Startup">
<!--StartupUri="MainWindow.xaml"-->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/CrossProjectUserControls;component/UIStandards.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
如您所见,我引用了 UISTandards.xaml,但在我的 MainProject 中的 UserControl 中,可以找到我的 none 样式。 有什么解决办法吗?