Element is already the child / System.Windows.Markup.XamlParseException --> PivotControl Error




- April 20, 2015

Rest of the Story:

I have been working with Silverlight PivotViewer control and recently came across problem with the display of the control.  I have a solution with multiple projects one of which is a Silverlight application.  Everything would compile without any error.  When browsing to the form/page with the control I would immediately get an exception on the InitializeComponent(); method within the Silverlight UserControl.  This method is called before loading any pivot cxml data.

I have seen this component through odd errors or fail to load when there is javascript errors on the page or a failure of any ajax method.  The error that is thrown is as follows…

{System.Windows.Markup.XamlParseException: Set property 'System.Windows.FrameworkElement.Style' threw an exception. [Line: 15 Position: 43] ---> System.Windows.Markup.XamlParseException: Element is already the child of another element. [Line: 0 Position: 0]
at MS.Internal.XcpImports.CreateFromXaml(UnmanagedMemoryStream stream, String sourceAssemblyName, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers)
at System.Windows.Controls.Control.GetBuiltInStyle(IntPtr nativeTarget, IntPtr& nativeStyle)
--- End of inner exception stack trace ---
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at PivotView.Viewer.InitializeComponent()
at PivotView.Viewer..ctor()}


If searching Google you will see a number of posts on errors surrounding ‘XamlParseException’, ‘Styles’, ‘Element is already the child’ etc.  Without help from these posts, I finally came across the solution.  I found the error was the result of some missing references in the Silverlight project.  The components missing can be found in C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Bin (which is installed as part of the most recent Silverlight Toolkit).  References to the following components was necessary.

System.Windows.Controls.Layout.Toolkit.dll System.Windows.Controls.Toolkit.dll System.Windows.Controls.Toolkit.Internals.dll

If you're having issues, rename the xap file (adding .zip) and take a look to ensure those dlls are included.

I thought I would post this information for others as well as a reminder to myself.  If you have any questions, let me know.