简体   繁体   English

WPF UserControl库中的ResourceDictionary不起作用

[英]ResourceDictionary in WPF UserControl library doesn't work

I create an UserControl library in wpf and want to define some general styles. 我在wpf中创建了一个UserControl库,并希望定义一些常规样式。 So I've added an global.xaml: 所以我添加了一个global.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:String x:Key="DefaultSearchImage">
        pack://application:,,,/WPF_Controls;component/Images/Search.png
    </system:String>
</ResourceDictionary>

I want to avoid this 我想避免这种情况

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

in every user control for performance reasons. 在每个用户控件中出于性能原因。 So I've found one solution at this site: 因此,我在此站点找到了一种解决方案:

Using Shared Resourec via static Dictionary 通过静态字典使用Shared Resourec

My DictionaryLoaderClass: 我的DictionaryLoaderClass:

internal static class SharedDictionaryManager
{
    internal static ResourceDictionary SharedDictionary
    {
        get
        {
            if (_sharedDictionary == null)
            {
                var resourceLocater =
                    new Uri(@"/pack://application:,,,/WPF_Controls;component/Styles/Globals.xaml", UriKind.Relative);
                _sharedDictionary =
                    (ResourceDictionary)Application.LoadComponent(resourceLocater);
            }
            return _sharedDictionary;
        }
    }


    private static ResourceDictionary _sharedDictionary;
}

and in every user control's constructor: 在每个用户控件的构造函数中:

public SearchPanel()
{
    InitializeComponent();
    Resources.MergedDictionaries.Add(SharedDictionaryManager.SharedDictionary);
}

But I always get an exception at runtime that the key "DefaultSearchImage" can't be found... 但是我总是在运行时遇到一个异常,即找不到键“ DefaultSearchImage” ...

If you want it to be loaded for every user control as a global style, why not just load it into the window resources? 如果要以全局样式将其加载给每个用户控件,为什么不将其加载到窗口资源中呢? Anything added to the tree will then inherit the applicable styles: 然后,添加到树中的所有内容都会继承适用的样式:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="800">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles/Globals.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
...content including UserControls
</Window>

You can then access it as a static resource: 然后,您可以将其作为静态资源进行访问:

Property="{StaticResource ResourceKey=DefaultSearchImage}"

and in the code-behind: 并在后面的代码中:

(String)Resources["DefaultSearchImage"];

or non UI code: 或非UI代码:

(String)Application.Current.Resources["DefaultSearchImage"];

Though I'd suggest something like this: 虽然我建议这样的事情:

<system:String x:Key="DefaultSearchImagePath">pack://application:,,,/WPF_Controls;component/Images/Search.png</system:String>
<BitmapImage x:Key="DefaultSearchImage" UriSource=DefaultSearchImage />

This will also allow re-uses of the user control to behave as anyone expects. 这也将允许用户控件的重用行为达到任何人的期望。 If you, or another developer on the project want to change that image, they just need to put their own style up there without it being overridden run-time later on (who's going to think to look there?) 如果您或项目中的其他开发人员想要更改该图像,那么他们只需要在此处放置自己的样式,而不会在以后的运行时被覆盖(谁想去那里看?)

It's a great strength of WPF. 这是WPF的强大优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM