English 中文(简体)
Element is already the child of another element error in Silverlight App.xaml
原标题:

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

最佳回答

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.

问题回答

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

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

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

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.





相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签