locked
StaticResource not working RRS feed

  • Question

  • Here is my code:

    <UserControl ...>
    	
    	<Grid ...>
    		...
    		<TextBlock ... Foreground="{StaticResource ThemeColor}" ... />
    		...
    	</Grid>
    	<UserControl.Resources>
    		<SolidColorBrush x:Key="ThemeColor" Color="#AB332B" />
    	</UserControl.Resources>
    </UserControl>
    


    When I have the code open in the VS 11 Designer, it works completely fine.

    But when I try to debug my project (a Metro style app), it crashes on the call to parse the XAML and gives an error about it can't find the resource with name/key %0. It gives the line and character of the place where I set the Foreground property, and it works fine when I don't use a resource, so I know that my resource reference is the problem. I use this Brush several places in the file, and I can't seem to use a DynamicResource. StaticResource fails at runtime. Why does this happen and how can I fix it?

    Thursday, December 15, 2011 12:39 AM

Answers

  • Hi Kendfrey,

    We're using a one-pass parser here, so the Resources need to be declared before they are used.  If you move the Resources definition block before the Grid it should work:

       <UserControl.Resources>
            <SolidColorBrush x:Key="ThemeColor" Color="#AB332B" />
        </UserControl.Resources>
        
       <Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
            <TextBlock Foreground="{StaticResource ThemeColor}" Text="Lorem ipsum dolor sit amet" />
        </Grid>
    
    

     

    --Rob
    • Marked as answer by kendfrey Friday, December 16, 2011 2:45 AM
    Friday, December 16, 2011 2:24 AM
    Moderator
  • I don't see where in that page the declaration is after the usage.  The only place I see a Resources block it is out of context and not explicit about where in the XAML file it would be placed, but it is used in the following block so if you followed the page in order the Resources would be before the Binding. 

    This could definitely be made more clear.

    By the way, if you find any other documentation like this that confuses you please report it with the Feedback tool.  There is an area for documentation errors at the bottom under Windows Kits\Software Development Kit\Documentation inaccuracies.

    --Rob

    • Marked as answer by kendfrey Friday, December 16, 2011 1:34 PM
    Friday, December 16, 2011 3:51 AM
    Moderator

All replies

  • Hi Kendfrey,

    We're using a one-pass parser here, so the Resources need to be declared before they are used.  If you move the Resources definition block before the Grid it should work:

       <UserControl.Resources>
            <SolidColorBrush x:Key="ThemeColor" Color="#AB332B" />
        </UserControl.Resources>
        
       <Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
            <TextBlock Foreground="{StaticResource ThemeColor}" Text="Lorem ipsum dolor sit amet" />
        </Grid>
    
    

     

    --Rob
    • Marked as answer by kendfrey Friday, December 16, 2011 2:45 AM
    Friday, December 16, 2011 2:24 AM
    Moderator
  • Thank you. That answered my question, but brought up another one. Why does the example app given by Microsoft in http://msdn.microsoft.com/en-us/library/windows/apps/br211380.aspx have the declaration after the usage? I think I had it running, but not positive.
    Friday, December 16, 2011 2:47 AM
  • I don't see where in that page the declaration is after the usage.  The only place I see a Resources block it is out of context and not explicit about where in the XAML file it would be placed, but it is used in the following block so if you followed the page in order the Resources would be before the Binding. 

    This could definitely be made more clear.

    By the way, if you find any other documentation like this that confuses you please report it with the Feedback tool.  There is an area for documentation errors at the bottom under Windows Kits\Software Development Kit\Documentation inaccuracies.

    --Rob

    • Marked as answer by kendfrey Friday, December 16, 2011 1:34 PM
    Friday, December 16, 2011 3:51 AM
    Moderator