WP7 opens and closes immediately

Feb 10, 2012 at 1:40 AM

I am doing the pinball walkthrough and I find that as soon as I add the first PhysicsObjectBehavior (to the rect) to the flipper child control the applications closes as soon as it starts with the following exception.  I am allowed to add a PhysicsObjectBehavior to the flipper (and that will run) however I am blocked from adding physics objects to any other component of the flipper.  The same thing works in WPF so I am wondering if there is something special for WP7 that isn't called out in the example.

The error is similar to this http://physicshelper.codeplex.com/workitem/9728 but the steps are not the same; I am following the example to the letter and starting out with a user control, not changing after the fact and since it works in Silverlight and not WP7 I suspect the issue is something different.

Any ideas?

System.InvalidOperationException was unhandled
  Message=Element is already the child of another element.
       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1 collection, CValue value)
       at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection`1 collection, DependencyObject value)
       at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value)
       at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value)
       at System.Windows.PresentationFrameworkCollection`1.Add(UIElement value)
       at Spritehand.FarseerHelper.PhysicsSprite..ctor(World physicsSim, Canvas parentCanvas, UIElement element, List`1 points, Boolean showDebug, Single defaultFriction, UIElement boundaryElement, Boolean disableCollisionEvents, Boolean forceRectangle)
       at Spritehand.FarseerHelper.PhysicsControllerMain.AddPhysicsBody(PhysicsObjectMain physObject)
       at Spritehand.PhysicsBehaviors.PhysicsObjectBehavior.controller_Initialized(Object source)
       at Spritehand.FarseerHelper.PhysicsControllerMain.InitializeObjects()
       at Spritehand.FarseerHelper.PhysicsControllerMain._timerInitialize_Completed(Object sender, EventArgs e)
       at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Feb 10, 2012 at 1:19 PM

You need to be careful about where you put PhysicsObjectBehavior's (and other behaviors) - they cannot be "nested".

So you might have a Canvas with a PhysicsObjectBehavior, and inside that _another_ Canvas with a PhysicsObjectBehavior?

Instead they should be just "one level deep" - so a PhysicsControllerBehavior at the top level "game canvas" and then inside that one or more usercontrols containing PhysicsObjectBehavior's, or one or more Canvas elements with PhysicsObjectBehaviors.

Hope that helps?


Feb 10, 2012 at 6:05 PM

I am very certain I am following the demo project exactly.  If I create a Silverlight project there is no error when I create the user control, it is only when I try the same steps but in a WP7.1 project that the exception is seen.  It appears to be something specific to using the library within a WP project.

I'm using the left flipper control as a test; works in Silverlight, fails in WP7.



Feb 10, 2012 at 11:32 PM

I found a solution but cannot explain why it makes a difference...

step 8 says: "Right-click the flipper Path and select Group Into/Canvas" and if I perform that step in a WP7.1 project it results in the exception above, leaving out the step doesn't appear to have any visual effect but the app can run without exception.  I noticed looking at the visual inheritence what the step results in a canvas within a canvas and I wonder if that has something to do with the error or not but at lease I found a solution and can move on.

VERY nice library by the way; excellent job!