XAML 资源字典在用户控件库中

3

如何在用户控件库中定义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’”,我是否漏掉或忘记了什么?
感谢您的帮助。
3个回答

4
您需要将您的ResourceDictionary包含或合并到UserControl.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}"

0
请在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文件“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中,没有找到任何样式。有什么解决办法吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接