问题描述
我正在尝试对动态创建的曲线上的自定义用户控件进行动画处理.
我需要更改该用户控件的3个属性,分别称为.X,.Y和.Angle.
我使用3个DoubleAnimationUsingKeyFrames,每个属性一个,但是我不知道如何在SetTargetProperty中引用属性.
我尝试使用2个仅属性X和Y来代替用户控件的Left和Top属性,并通过以下方式进行设置:
Storyboard.SetTargetProperty(Anim_X,New PropertyPath(((Canvas.Left)"))
Storyboard.SetTargetProperty(Anim_Y,New PropertyPath(((Canvas.Top)"))
可以,但是如何设置角度旋转?
谢谢
I''m trying to animate a Custom User Control on a curve dynamically created.
I need to change 3 properties called .X, .Y and .Angle of that user control.
I use 3 DoubleAnimationUsingKeyFrames, one for each property, but I don''t know how to reference properties in SetTargetProperty.
I tryed with 2 only properties X and Y using instead Left and Top properties of my user control and setting them in this way:
Storyboard.SetTargetProperty(Anim_X, New PropertyPath("(Canvas.Left)")
Storyboard.SetTargetProperty(Anim_Y, New PropertyPath("(Canvas.Top)")
It works but how to set Angle rotation?
Thanks
推荐答案
<usercontrol x:class="Coach2D_SL.UC1" xmlns:x="#unknown">
Xmlns … >
<stackpanel x:name="Sp" background="Transparent" margin="100,100,0,0">
<border name="Border1" background="transparent" borderbrush="transparent" borderthickness="2" x:uid="mbi">
<grid>
Width="56" Height="56" />
</grid>
</border>
</stackpanel>
</usercontrol>
代码隐藏
CodeBehind
Partial Public Class UC1
Inherits UserControl
Public WriteOnly Property Position As Integer()
Set(value As Integer())
Sp.Margin = New Thickness(value(0) - image1.Width / 2, value(1) - image1.Height / 2, 0, 0)
End Set
End Property
Public WriteOnly Property RotationAngle As Double
Set(value As Double)
Rotate.Angle = value
image1.RenderTransform = Rotate
End Set
End Property
End Class
MainPage.xaml
MainPage.xaml
. . .
xmlns:uc1="clr-namespace:SilverlightApplication1"
. . .
代码隐藏
CodeBehind
Imports SilverlightApplication1.UC1
Partial Public Class MainPage
Inherits UserControl
Dim Uc1 As New UC1
Private Sub Canvas1_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Canvas1.Loaded
Uc1.Position = {70, 150}
Canvas1.Children.Add(Uc1)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
'' Prepare animation
Actpts_X = New DoubleCollection
Actpts_Y = New DoubleCollection
ActAng = New DoubleCollection
''
Dim Anim_X As New DoubleAnimationUsingKeyFrames
Dim Anim_Y As New DoubleAnimationUsingKeyFrames
Dim AnimAng As New DoubleAnimationUsingKeyFrames
''
Dim d As New Duration(TimeSpan.FromSeconds(T))
Anim_X.Duration = d
Anim_Y.Duration = d
AnimAng.Duration = d
'' Create Frames
For i = 0 To PolyLine1.Points.Count - 1
Actpts_X.Add(PolyLine1.Points(i).X - Canvas1.Margin.Left)
Actpts_Y.Add(PolyLine1.Points(i).Y - Canvas1.Margin.Top)
Anim_X.KeyFrames.Add(New DiscreteDoubleKeyFrame() With { _
.Value = PolyLine1.Points(i).X, _
.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i / speed)) _
})
Anim_Y.KeyFrames.Add(New DiscreteDoubleKeyFrame() With { _
.Value = PolyLine1.Points(i).Y, _
.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i / speed)) _
})
AnimAng.KeyFrames.Add(New DiscreteDoubleKeyFrame() With { _
.Value = RotAng(i), _
.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i / speed)) _
})
Next
Dim SB As New Storyboard
Canvas1.Resources.Add("unique_id", SB)
SB.Duration = d
SB.Children.Add(Anim_X)
SB.Children.Add(Anim_Y)
SB.Children.Add(AnimAng)
''
Storyboard.SetTargetProperty(Anim_X, New PropertyPath("(Uc1.X)"))
Storyboard.SetTargetProperty(Anim_Y, New PropertyPath("(Uc1.Y)"))
Storyboard.SetTargetProperty(AnimAng, New PropertyPath("(Uc1.Angle)"))
''
Storyboard.SetTarget(Anim_X, Uc2)
Storyboard.SetTarget(Anim_Y, Uc2)
Storyboard.SetTarget(AnimAng, Uc2)
''
SB.Begin()
End Sub
这篇关于自定义用户控件的Silverlight SetTargetProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!