WPF Dynamic Resource: Warning Message - "The resource 'resource' could not be resolved."
Asked Answered
T

1

12

Basically, I have a UserControl that uses a DynamicResources. The application compiles and runs fine using that resource, but it's a bit annoying to see this warning message. It makes me wonder if I'm doing it right and it's just a false positive, or if I'm doing it wrong and happen to slip through the cracks in it working.

<Grid Background="{DynamicResource AppDefaultBackgroundColor}">
...
</Grid>

So, if I used it correctly, how can I get rid of this warning message?
If I did not use it appropriately, what should it look like so I don't get a warning?

P.S. My application works and runs fine, but I prefer to not have any compilation warnings when compiling.

Tunstall answered 31/3, 2011 at 20:13 Comment(6)
Where is the resource being loaded from? You may just need to change the compilation order of your projects.Dosage
From the App.xaml, which happens to be the same project that is housing this UserControl.Tunstall
If you are using a designer: Throw it out, get's rid of 90% of all false warnings and errors. (Note: That number is made up and may not be completely accurate)Spellbound
I am using VS 2010 too, but i do not use its designer. (Right click a XAML file -> Open with... -> Source Code (Text) Editor -> Set as Default -> OK) I never really get any warnings that way and it happened a few times already that people said something about warnings unknown to me here on SO when i offered a solution the designer just could not handle.Spellbound
Why is it a DynamicResource? If the resource is in your App.xaml and the usercontrol is in the same assembly; why not make it a StaticResource!Volition
Per the comment here "A DynamicResource assigns an Expression object to the property during loading but does not actually lookup the resource until runtime when the Expression object is asked for the value. This defers looking up the resource until it is needed at runtime." So ignore the IDE warning. It'll disappear if resolved correctly at runtime!*Hays
C
6

You can map your resource like this

<Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="..\Resources\Theme.xaml" />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Resources>

Theme.xaml must contain 'AppDefaultBackgroundColor' like this

 <Color x:Key="AppDefaultBackgroundColor">#FF77C6FB</Color>
Chanda answered 30/11, 2011 at 17:38 Comment(1)
I'm guessing the **Source is from attempting to show bolded text inside a code block? Just wondering since it doesn't like that syntax in the designer.Eliseelisee

© 2022 - 2024 — McMap. All rights reserved.