7
Vote

User control Exception 'Element is already the child of another element.'

description

using farseer 4.0 and blend 4.0
1 Create new windows phone application
2 Change layout type to canvas
3 Add PhysicsControllerBehaviour
4 Add rectangle with PhysicsObjectBehaviour
5 Select rectangle and PhysicsObjectBehaviour and Right click 'Make into User Control'
6 Application will not run
7 Debugging in VS2010 reveals a 'Element is already the child of another element.' exception

comments

crossguard wrote Apr 5, 2011 at 9:20 PM

asume UserControl1 structure goes like this:
[UserControl]->LayoutRoot(canvas)->cnvItem(canvas)->[PhysicsObjectBehavior]

try this:
UserControl1 userControl1 = new UserControl1();
// add userControl to main canvas
LayoutRoot.Children.Add(userControl1);
// add the physics for this ui and get the Sprite
PhysicsController.AddPhysicsBodyForCanvasWithBehaviors(userControl1.LayoutRoot);
// you can remove the userControl because PhysicsController just duplicate it for you as a PhysicsSprite
LayoutRoot.Children.Remove(userControl1);