views:

596

answers:

3

I am trying to alias a resource in XAML, as follows:

<UserControl.Resources>

    <StaticResourceExtension x:Key="newName" ResourceKey="oldName"/>

</UserControl.Resources>

oldName simply refers to a resource of type Image, defined in App.xaml.

As far as I understand, this is the correct way to do this, and should work fine. However, the XAML code gives me the superbly unhelpful error:

"The application XAML file failed to load. Fix errors in the application XAML before opening other XAML files."

This appears when I hover over the StaticResourceExtension line in the code (which has a squiggly underline). Several other errors are generated in the actual Error List, but seem to be fairly irrelevant and nonsenical (such messages as "The name 'InitializeComponent' does not exist in the current context"), as they all disappear when the line is removed.

I'm completely stumped here. Why is WPF complaining about this code? Any ideas as to a resolution please?

Note: I'm using WPF in .NET 3.5 SP1.

Update 1:
I should clairfy that I do receive compiler errors (the aforementioned messages in the Error List), so it's not just a designer problem.

Update 2:
Here's the relevant code in full...

In App.xaml (under Application.Resource):

<Image x:Key="bulletArrowUp" Source="Images/Icons/bullet_arrow_up.png" Stretch="None"/>
<Image x:Key="bulletArrowDown" Source="Images/Icons/bullet_arrow_down.png" Stretch="None"/>

And in MyUserControl.xaml (under UserControl.Resources):

<StaticResourceExtension x:Key="columnHeaderSortUpImage" ResourceKey="bulletArrowUp"/>
<StaticResourceExtension x:Key="columnHeaderSortDownImage" ResourceKey="bulletArrowDown"/>

These are the lines that generate the errors, of course.

+1  A: 

Try as I might, I cannot reproduce your problem. I think there is more at play than the code you have posted. This works fine for me:

App.xaml:

<Application x:Class="WpfApplication1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <Image x:Key="bulletArrowUp" Source="Leaves.jpg" Stretch="None"/>
     <Image x:Key="bulletArrowDown" Source="Leaves.jpg" Stretch="None"/>
    </Application.Resources>
</Application>

Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <local:UserControl1/>
</Window>

UserControl1.xaml:

<UserControl x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <UserControl.Resources>
     <StaticResourceExtension x:Key="columnHeaderSortUpImage" ResourceKey="bulletArrowUp"/>
     <StaticResourceExtension x:Key="columnHeaderSortDownImage" ResourceKey="bulletArrowDown"/>
    </UserControl.Resources>
    <ContentControl Content="{StaticResource columnHeaderSortUpImage}"/>
</UserControl>

I added the Leaves.jpg image to my project and it displayed just fine.

HTH, Kent

Kent Boogaart
Thanks for the reply. And no, unfortunately actual compiler errors are generated (nonsense messages that all go away if I don't define the aliases). Not sure if it makes a difference, but did you notice that the resources I'm using are `Image` elements?
Noldorin
Can you post your full code?
Kent Boogaart
@Kent: Updated my original question. Thanks for staying with this...
Noldorin
I've updated my answer - it's still working fine for me. I've even gotten an image to display inside the UserControl.
Kent Boogaart
@Kent: I repdroduced your example and it seems to be working fine for me too. (Should have tried that earlier, to be honest!) To be precise here, I am actually nesting the resources in App.xaml in a ResourceDictionary, but this doesn't make a difference in my example project. This issue has really stumped me... Does the 'InitializeComponent' error give you any hints perhaps?
Noldorin
@Kent: I've also just noticed that the UserControl.g.cs file does not get generator when I'm getting the errors - this is probably what is causing them. Why it doesn't get generated then, I have no idea.
Noldorin
+1 for the effort btw.
Noldorin
+1  A: 

I tried using the same setup as Kent and received the following error:

Property 'Resources' does not support values of type 'System.Windows.Controls.Image'.

I also tried a few other types and got the same thing (only with a different full-qualified type name).

I was only able to re-key an Image in the <UserControl.Resources> in the following way:

<UserControl.Resources>
    <Image x:Key="myimg"
           Source="{Binding Source={StaticResource appimg}, Path=Source}"
           Stretch="{Binding Source={StaticResource appimg}, Path=Stretch}"/>
</UserControl.Resources>

I have to wonder: Why? Resources weren't designed to be re-keyed. I have a suspicion that what you are ultimately trying to accomplish could be done better and easier in some other way. Possibly with setters in a style.

Joel B Fant
I received the same error, but I noticed that the project actually built successfully. I was able to run the project and it worked as desired. I think your particular error may be coming from a XAML validator inside of VS that has no bearing on the build process.
Bryce Kahle
You're right. So it wasn't my eyes playing tricks on when I thought it said 'Build succeeded' a split second before the error panel popped up.
Joel B Fant
A: 

Good comments, as detail of WPF designer load issue is very clear but still answer confusing ZAHID NAWAZ

Zahid Nawaz