This post is a consequence of two previous posts:
The application I made only adds ellipses and rectangles (at the same time) to the main stage when a button is clicked.
Here is the code for the ball.xaml:
<Ellipse x:Name="lb" Fill="#FF33D01E" Stroke="Black" Height="30" Width="30">
<TextBlock Height="30" TextWrapping="Wrap" Text="0" Width="30" TextAlignment="Center" FontSize="18.667" FontWeight="Bold" Foreground="#FFFBEEEE" Canvas.Top="2"/>
And here you are the square.xaml:
<Canvas x:Name="LayoutRoot" Width="25" Height="25">
<Rectangle Fill="#EAF71919" Height="25" Stroke="Black" Width="25">
<TextBlock Height="25" TextWrapping="Wrap" Text="0" Width="25" TextAlignment="Center" FontSize="18.667" FontWeight="Bold" Foreground="#FFFBEEEE" />
As you can see the idea here is to add a ball and a square with a number 0 inside of them. The problem here is that with this xaml structure the only thing that will be under the physics influence will be the rectangle and the ellipse but not the Texblocks.
My first idea was to had the PhysicsObjectBehaviour to the main Canvas "LayoutRoot" but it does not work!
Indeed, it throws an exception if any previous ball or square was added into the main stage when the application loads! I put a square and then a ball previously via xaml and the famous error message appeared:
Could not determine the outline of UIElement sprite_2. Could not find a point within its boundaries
I forgot to post the button event handler code:
Private Sub Button_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btAdd.click
'reference to main controller
Dim p = CType(screen.GetValue(PhysicsControllerMain.PhysicsControllerProperty), PhysicsControllerMain)
Dim c As New square
c.SetValue(Canvas.LeftProperty, Convert.ToDouble(New Random().Next(0, 250)))
Dim o1 = p.AddPhysicsBodyForCanvasWithBehaviors(c.LayoutRoot)
Dim b As New ball
b.SetValue(Canvas.LeftProperty, Convert.ToDouble(New Random().Next(0, 500)))
Dim o = p.AddPhysicsBodyForCanvasWithBehaviors(b.LayoutRoot)
Next thing I tried is to add the PhysicsObjectBehavior to the UserControl itself but this has 2 different sideffects:
The programmatically added objects won't have physics.
The xaml added objects will have physics but the ellipse (again these holy ellipses) won't behave correctly. Instead of a ellipse they move like a square. Maybe they are getting the boundaries of the usercontrol
and it is a square?
then I tried to use the BoundaryElement property (targeting the ellipse and the rectangle respectively) of the PhysicsObjectBehavior but again a weird behaviour occurs: Now the elements appear in a border
of the screen and fall down beyond the ground object dissapearing from the screen!!! but again,
when I try to add them programmatically they appear but without physics.
The only option for me is to create a new panel inside the usercontrol containing both the ellipse/rectangle and the text and then assign the physicsObjectBehavior to the new panel... but... in the case of the
ellipses we have againg the 2 problem I noted before: they behave like squares!!
All seem to work now but they behave like square when they roll down the ground!!
And then again I try the BoundaryElement property with the following results (I applied it to the rectangle and the ellipse in square.xaml and ball.xaml respectively):
The square works great with its associated textblock moving along with it.
The ellipse throws the boundary error!
The questions here are:
How can I make these ellipses behave correctly?
Why the boundaryElement property is not working correctly with ellipses? Am I missing something?