首页 文章

用户控件库中的xaml ResourceDictionary

提问于
浏览
1

如何在用户控件库中定义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 回答

  • 0

    您必须包含或合并 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>
    

    然后,您可以使用UserControl内的字典中的资源

    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>
    

    希望有所帮助

  • -1

    好吧,所以我得到了一半的工作 . 在我的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中,我找不到任何样式 . 有解决方案?

相关问题