【问题标题】:xaml ResourceDictionary inside User Control Library用户控件库中的 xaml ResourceDictionary
【发布时间】:2016-04-06 09:31:50
【问题描述】:

如何在用户控件库中定义 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”与控件一起使用

 Style="{StaticResource NormalStyle}"

但 Visual Studio 显示“无法解析资源“NormalStyle”。” 我错过或忘记了什么吗?

谢谢你帮助我

【问题讨论】:

    标签: wpf user-controls resourcedictionary


    【解决方案1】:

    您必须将您的ResourceDictionaryUserControl.Resources 包含或合并,如下所示。在 Source 中为您的ResourceDictionary 提供路径。

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

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

    然后你可以在你的用户控件中使用字典中的资源

    Style="{StaticResource NormalStyle}"
    

    【讨论】:

      【解决方案2】:

      请在 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>
      

      希望有帮助

      【讨论】:

        【解决方案3】:

        好的,所以我做了一半。在我的 UserControlLibrary 中,我创建了一个新的 Xaml 文件“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 中,找不到任何样式。 有什么解决办法吗?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-03
          • 1970-01-01
          • 2016-01-12
          相关资源
          最近更新 更多