XAML - How do I create a DoubleAnimationUsingKeyFrames dynamically and play the storyboard with VB?

杀马特。学长 韩版系。学妹 提交于 2019-12-12 02:22:09

问题


I have 3 polygons that I am trying to "light up" (changing opacity) based on dynamically code. For instance, I have a long string of random numbers (021200112). I'm trying to create a dynamic storyboard with keyframes. I'm testing it with a single polygon and I am getting a target error. Your help is appreciated!!!

The polygons are created in the XAML (named Poly1, Poly2, Poly3).

Here is my error when it tries to play the storyboard:

WinRT information: Cannot resolve TargetProperty 1 on specified object.

Here is my code to test the animation on poly1.

Public Sub PlayStoryboard()

    Dim myDoubleAnimationUsingKeyFrames As New DoubleAnimationUsingKeyFrames()
    Dim myLinearDoubleKeyFrame As New LinearDoubleKeyFrame()
    Dim myLinearDoubleKeyFrame2 As New LinearDoubleKeyFrame()

    myLinearDoubleKeyFrame.Value = 0.2
    myLinearDoubleKeyFrame.KeyTime = TimeSpan.FromSeconds(1)

    myLinearDoubleKeyFrame2.Value = 1
    myLinearDoubleKeyFrame2.KeyTime = TimeSpan.FromSeconds(3)
    myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myLinearDoubleKeyFrame)
    myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myLinearDoubleKeyFrame2)

    myDoubleAnimationUsingKeyFrames.Duration = New Duration(TimeSpan.FromMilliseconds(5000))

    Dim myStoryboard = New Storyboard()
    myStoryboard.Children.Add(myDoubleAnimationUsingKeyFrames)

    Storyboard.SetTarget(myDoubleAnimationUsingKeyFrames, Poly1)
    Storyboard.SetTargetName(myDoubleAnimationUsingKeyFrames, Poly1.Name)
    Storyboard.SetTargetProperty(myDoubleAnimationUsingKeyFrames, Opacity)

    myStoryboard.Begin()

End Sub

回答1:


The target property is meant to be the name of the property to animate (or, more specifically, a PropertyPath, but that can be converted from a String).

Try the following:

Storyboard.SetTargetProperty(myDoubleAnimationUsingKeyFrames, "Opacity")


来源:https://stackoverflow.com/questions/11901998/xaml-how-do-i-create-a-doubleanimationusingkeyframes-dynamically-and-play-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!