user3688248 user3688248 - 6 months ago 31
Vb.net Question

WPF Geometry Rotatation Animation

I would like to rotate my custom shape with an animation.
When i run my application i get this message in my immediate window:


BindingExpression path error: 'Angle' property not found on 'object'
''MatrixTransform' (HashCode=16493105)'. null


My shape

Dim myPointerPath = New Path()
Dim myPointerPathGeometry = New PathGeometry()
Dim myPointerPathFigure = New PathFigure()
Dim leftLine As New LineSegment
Dim rightLine As New LineSegment

myPointerPathFigure.StartPoint = New Point(Me.left + (Me.width / 2), Me.top + (Me.width / 60))

leftLine.Point = New Point(Me.left * 1.8, Me.top)
rightLine.Point = New Point(Me.left + (Me.width / 2), Me.top - Me.width / 60)

myPointerPathFigure.Segments.Add(leftLine)
myPointerPathFigure.Segments.Add(rightLine)
myPointerPathGeometry.Figures.Add(myPointerPathFigure)
myPointerPath.Data = myPointerPathGeometry
myPointerPath.Stroke = Me.pointerColor
myPointerPath.Fill = Me.pointerColor

myCanvas.Children.Add(myPointerPath)

Me.pointerShape = myPointerPathGeometry


For a single line shape, this code would animate the rotation:

Public Sub drawPointerAtValue(ByVal p_value As Double)

Dim rotator = New RotateTransform(p_value)
Dim da = New DoubleAnimation(0, 180, New Duration(TimeSpan.FromSeconds(2)))
Dim storyboard = New Storyboard()

rotator.CenterX = pointerLine.X1
rotator.CenterY = pointerLine.Y1

storyboard.Children.Add(da)
storyboard.SetTarget(da, pointerLine)
storyboard.Duration = da.Duration
storyboard.SetTargetProperty(da, New PropertyPath("RenderTransform.Angle"))
storyboard.AccelerationRatio = 0.2
storyboard.DecelerationRatio = 0.2

da.to = p_value
storyboard.Begin()
End Sub


this is the code for the rotation animation of my custom shape (2 line shapes)

Public Sub rotateShape(ByVal p_value As Double)
Dim rotator = New RotateTransform(p_value)
Dim da = New DoubleAnimation(0, 180, New Duration(TimeSpan.FromSeconds(2)))
Dim storyboard = New Storyboard()

rotator.CenterX = Me.left + (Me.width / 2)
rotator.CenterY = Me.top

storyboard.Children.Add(da)
storyboard.SetTarget(da, pointerShape)
storyboard.Duration = da.Duration
storyboard.SetTargetProperty(da, New PropertyPath("Transform.Angle"))
storyboard.AccelerationRatio = 0.2
storyboard.DecelerationRatio = 0.2
da.To = p_value
storyboard.Begin()
End Sub

Answer

I got it working by changing the shape to a Polygon, instead of using a PathGeometry.

Dim rotator = New RotateTransform(Value)
    Dim da = New DoubleAnimation()
    Dim sb = New Storyboard()

    rotator.CenterX = centreX
    rotator.CenterY = centreY

    rotator.CenterX = centreX
    rotator.CenterY = centreY

    pointerShape.RenderTransform = rotator
    Storyboard.SetTarget(da, Me.pointerShape)
    Storyboard.SetTargetProperty(da, New PropertyPath("RenderTransform.Angle"))

    da.Duration = New Duration(TimeSpan.FromSeconds(2))
    da.to = toValue
    da.from = fromValue

    sb.AccelerationRatio = 0.2
    sb.DecelerationRatio = 0.2
    sb.Children.Add(da)
Comments