views:

2094

answers:

4

Hi,

I keep getting a strange error inside my App.xaml file: Element is already the child of another element.

My App.xaml file looks like this:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="Celerior.Annapurna.SL.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ProvisiorResourceDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

The error is reported for the entire ResourceDictionary element (from lines 5 to 9).

ProvisiorResourceDictionary.xaml contains a number of styles and templates. Nothing exciting in my opinion.

Does anyone know what is wrong?

Kind regards,

Ronald Wildenberg

+2  A: 

I found the cause of the problem myself, thanks to the hints provided in the comment by AnthonyWJones.

It appears everything inside a Silverlight resource dictionary must be shareable. The reason is that items inside a resource dictionary will (probably) be added at multiple locations in the control hierarchy.

I had two items inside my resource dictionary that were not shareable.

Ronald Wildenberg
TIP: if you're not sure which items are causing this error, just create a second Resources2.xaml referenced by the App.xaml and move over some files to it. make sure you recompile fully. this should allow you to determine which resources are non-sharable and causing the problem
Simon_Weaver
A: 

Yes but... How did you make these elements shareable from inside your resource dictionary?

tnf3251
I moved them from the resource dictionary to the page where I needed them. I'm not sure whether I duplicated these resources but I don't think I did. Probably moved them to a page base class.
Ronald Wildenberg
+1  A: 

Probably NOT an answer to this question but another common reason you can get this "Element is already the child of another element." error is if you are trying to load a resource, such as an image and you have got the filename wrong.

IE especially complains will complain about this.

<Image ToolTipService.ToolTip="Email customer" 
       Source="../images/FILE-THAT-DOESNT-EXIST.png"></Image>

However since this is related to resources there could possibly be a scenario where this would be an answer to this question :-)

Simon_Weaver
A: 

I had the very same problem, when I checked my Styles.xaml file, it had some elements with the same name, I changed the name of elements and made them unique, and the issue resolved :)

Zain Shaikh