Wpf 用户控件库中的xaml ResourceDictionary

Wpf 用户控件库中的xaml ResourceDictionary,wpf,user-controls,resourcedictionary,Wpf,User Controls,Resourcedictionary,如何在用户控件库中定义ResourceDictionary并通过Xaml代码访问它们 我创造了这样的东西: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Controls="clr-

如何在用户控件库中定义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
但是VisualStudio说“资源”NormalStyle“无法解决。” 我是错过了什么还是忘了什么


感谢您的帮助

您必须将您的
ResourceDictionary
UserControl.Resources
合并,如下所示。在源代码中,这里给出您的
资源字典的路径

<UserControl.Resources>
        <ResourceDictionary Source="MyResourceDictionary.xaml"/>
</UserControl.Resources>

请在App.xamal therfore中添加样式引用,以便在运行应用程序时加载。 当您添加任何外部资源文件时,您需要将其添加到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文件“UIStandards”,将我所有的样式都放在其中。现在在我的主要项目中,我希望能够访问这些样式,我认为我应该使用一个合并词典

我所做的是:

<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,但在我的主项目中的UserControl中,找不到我的样式。 有什么解决办法吗

Style="{StaticResource NormalStyle}"
 <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>
<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>