WPF StaticResource works, DynamicResource doesn't
Asked Answered
O

3

11

I have been trying for a day now, to no avail, to create a bunch of brushes in the theme then using them with DynamicResource in a custom control. What I did is this:

  • create the theme generic.xaml which contains styles (works)
  • add a dictionary to merge in generic.xaml to contain brushes used in the application (works)
  • make brushes have ComponentResourceKey keys (works)
  • make control use brushes as static resource (works)
  • make control use brushes as dynamic resource (DOESN'T WORK, the resource trace source says as much: System.Windows.ResourceDictionary Warning: 9 : Resource not found; )
  • add in App.Resources dynamically a brush with the same key (works with dynamic resource, it changes the colors, doesn't work with static resource, as expected)

So my problem is that I can't find any way to define the default values in the theme so that I can change them programatically in the application. How can StaticResource find the brush and DynamicResource not?!

I must add that I've created a static class holding the component resource keys as properties that I then use in the xaml as {x:Static UI:ResourceScheme.ControlBackgroundKey} for example. My problem seems similar to this one: ComponentResourceKey as DynamicResource problem only that if I replace the static property keys to the XAML markup for component resource key, it still doesn't work.

Can someone please help me out here? :(

Orola answered 21/8, 2010 at 9:31 Comment(0)
A
7

Here is the difference,

StaticResource loads at time of loading, this means that the resource key that you are using, must be lexically defined before the usage.

So, static resource in case of custom control must be defined only above the control definition in the same generic.xaml file. So if you put your brushes in different xaml, it will certainly not work in case of static resource.

This is the reason, unless the other resources of type xaml is included in the form of some sort of import at a time of complile in same file, you can not use static resource in the file. It simply means that the file/component/control's actual xaml some how should contain actual reference of static resource you use.

Now I have my doubt of why DynamicResource will not work, that is because probably DynamicResource will only look in the Application's (where the control is use) ResourceDictionary but not generic.xaml.

I am not 100% sure but I feel if you define a custom control and if you use DynamicResource then your resources has to be in the Application's Resource Dictionary or the parent container of your control's resource dictionary, but it can not be in generic.xaml.

Because DynamicResource will only look up for keys in the logical tree of control's runtime and thats why it may not find resources that are in generic.xaml unless generic.xaml is explicitly added in Application.Resources.

Summary: StaticResource must be available lexically before in the same file at compile time, resources will be available in Application.Resources dictionary, it still can find in logical tree but at a compile time only in the same dll or same generic.xaml.

DynamicResource must will be searched in Application.Resources and in the logical tree of the control at runtime.

For more reference, please check Resources Overview

Appose answered 21/8, 2010 at 10:3 Comment(6)
Thanks for the answer, even if I don't feel it is any good :), but the thing is this: in order to access a resource defined in a theme, one must use a ComponentResourceKey. Simplifying the problem, I get to define a resource in the theme with a ComponentResourceKey defined using a type in another assembly, and then I can't find the resource using FindResource. Using a type from the same assembly as the theme file, I get to find the resource.Orola
I feel this is related to some sort of assembly loading order, but how can you not load an assembly if you are trying to find a resource using a type from that assembly?Orola
If resource x is in generic.xaml, even if it is loaded and in memory, dynamic resource will not find it, it will only find in the resource dictionaries of its own logical parental tree. Dynamic Resource loading uses FindResource but StaticResource does not use FindResource method instead resource references are set at time of compilation.Appose
Ok, I understand that. The question is why did FindResource fail? I mean, changing the type of the component resource type makes it either fail of succeed. Why is that?Orola
@AkashKava No offense, but you wrote a wall of text when actually you meant to say "I don't know"Tocharian
Important note: DynamicResources must be merged in Application.Current.Resources exclusively, any other resource dictionary in the logical tree of control (e.g. Window) in does not seem to work. Moreover, the dynamic resources need to be merged rather than added.Spermatozoon
O
7

Finally fixed it. It appears that having the type for the component resource key in another assembly caused the whole issue. Let me summarize:

  • there is a resource class that holds the ComponentResourceKeys as static properties. The type used in the constructor of the resource keys is the type of this class. This is in the Resources assembly.
  • there is a theme for the custom controls in another assembly, the Controls assembly, that defines some brushes using as key the properties of the resource class: {x:Static Namespace:ResourceClass.ResourceKeyProperty}
  • in the same theme, templates for controls use the brushes as dynamic resources: {DynamicResource {x:Static Namespace:ResourceClass.ResourceKeyProperty}}
  • there is also an application that uses these controls and that dynamically adds custom brushes in the application resources. These brushes have the same keys as the ones in the theme.

The end result for this is:

  • the controls do not use the brushes initially
  • the controls do use the brushes added in the application resources
  • the controls use the brushes initially if StaticResource is used in the theme, but then the application resources are ignored

The solution for this seems to be to move the resources class in the controls library.

As I still do not know why this is happening, this question remains open, even if slightly changed: why doesn't it work in the first scenario?

Orola answered 21/8, 2010 at 12:27 Comment(0)
T
2

I tried to reproduce your problem to test out a few theories of why it doesn't work, however I couldn't reproduce the problem. The setup appears to work.

So instead of describing a solution I will describe the non-working repro setup.

The solution

Target framework: 4.6

Projects

  • Controls
    • references
      • Resources
    • files
      • Theme.xaml
  • Resources
    • files
      • Keys.cs
  • WpfApplication
    • references
      • Controls
      • Resources
    • files
      • MainWindow.xaml

File contents

Theme.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:r="clr-namespace:Resources;assembly=Resources">
    <Color x:Key="{x:Static r:Keys.PrettyColor}">Red</Color>
    <SolidColorBrush x:Key="{x:Static r:Keys.PrettyBrush}" 
                     Color="{DynamicResource {x:Static r:Keys.PrettyColor}}" />
</ResourceDictionary>

Keys.cs

namespace Resources {
    using System.Windows;

    public static class Keys {
        public static readonly ComponentResourceKey PrettyBrush =
            new ComponentResourceKey(typeof(Keys), Ids.PrettyBrush);

        public static readonly ComponentResourceKey PrettyColor =
            new ComponentResourceKey(typeof(Keys), Ids.PrettyColor);
    }

    public static class Ids {
        public const string PrettyBrush = "PrettyBrush";
        public const string PrettyColor = "PrettyColor";
    }
}

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:resources="clr-namespace:Resources;assembly=Resources"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Controls;component/Theme.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <Color x:Key="{x:Static resources:Keys.PrettyColor}">Blue</Color>
        </ResourceDictionary>
    </Window.Resources>
    <Border Background="{DynamicResource {x:Static resources:Keys.PrettyBrush}}" />
</Window>

And for the lazy people, here's a screenshot:

enter image description here

Tocharian answered 20/12, 2015 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.