'Element is already the child of another element' error in Silverlight App.xaml
Asked Answered
A

5

8

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

Alcmene answered 13/11, 2009 at 8:32 Comment(3)
Can you provide other info. What is in ProvisorResourceDictionary.xaml. Is it also used elsewhere? Are there some home grown components in it doing some odd things in their constructors or property set procedures?Cassell
I added some info to my question about the contents of ProvisiorResourceDictionary. It contains just styles and templates. Maybe I'll try removing code from it piece by piece to see if that helps.Alcmene
I have an idea. I have a DataTemplate that I add to two different template selectors (custom classes that detect the template to use based on some info). Is it impossible to reuse DataTemplate's??Alcmene
A
6

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.

EDIT: In WPF, you can use the x:Shared attribute on objects inside a resource dictionary to force WPF to create a new instance for every resource retrieval. Unfortunately, Silverlight does not support this attribute.

Alcmene answered 13/11, 2009 at 12:54 Comment(2)
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 problemKass
I had this error and turned out one of our developers put x:Name instead of x:Key on one of the elements.Weil
K
3

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 :-)

Kass answered 11/8, 2010 at 3:30 Comment(0)
C
1

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 :)

Concertmaster answered 31/10, 2010 at 14:0 Comment(0)
C
0

It seems this also oocurs if you try to put a Storyboard with a Key inside a ResourceDictionary, instead of putting it inside a ControlTemplate

This is because Storyboard are stateful objects and can't be reused (they track if they're started, paused etc.)

Cholent answered 31/1, 2014 at 12:2 Comment(0)
S
0

The advice by @Simon_Weaver in the answer

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

led me to find this item, the first one I removed actually; it was an unused path object:

<Path x:Key="RightArrowPath" .. />

as the culprit. Accroding to Resource Dictionaries #Objects for Resource Dictionary Usage, it does not list a Path as a shareable item.

Smyth answered 29/6, 2015 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.