我正在尝试将 XAML Storyboard 转换为 Storyboard 背后的代码。但我遇到了一个异常(exception)
Cannot resolve all property references in the property path
'(UIElement.RenderTransform).(myTransformGroup.Children)[1].(TranslateTransform.Y)'.
Verify that applicable objects support the properties.
我搜索了几个网站,但他们也是这样做的。我不明白我哪里错了。
XAML:
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="r1">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0.828"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.828"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="r1">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="8.621"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="8.621"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
C# 代码:
public void myAnimation(Rectangle myRectangle)
{
this.UnregisterName(myRectangle.Name);
ScaleTransform myScaleTransform = new ScaleTransform();
TranslateTransform myTranslateTransform = new TranslateTransform();
TransformGroup myTransformGroup = new TransformGroup();
myTransformGroup.Children.Add(myScaleTransform);
myTransformGroup.Children.Add(myTranslateTransform);
myRectangle.RenderTransform = myTransformGroup;
this.RegisterName(myRectangle.Name, myTransformGroup);
DoubleAnimationUsingKeyFrames myDoubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();
EasingDoubleKeyFrame myEasingDoubleKeyFrame = new EasingDoubleKeyFrame();
Storyboard myStoryboard = new Storyboard();
Storyboard.SetTargetName(myDoubleAnimationUsingKeyFrames, myRectangle.Name);
Storyboard.SetTargetProperty(myDoubleAnimationUsingKeyFrames, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(0);
myEasingDoubleKeyFrame.Value = 1;
myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);
myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(0.4);
myEasingDoubleKeyFrame.Value = 0.828;
myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);
myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(1);
myEasingDoubleKeyFrame.Value = 0.828;
myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);
myStoryboard.Children.Add(myDoubleAnimationUsingKeyFrames);
Storyboard.SetTargetName(myDoubleAnimationUsingKeyFrames, myRectangle.Name);
Storyboard.SetTargetProperty(myDoubleAnimationUsingKeyFrames, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.X)"));
myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(0);
myEasingDoubleKeyFrame.Value = 0;
myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);
myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(0.4);
myEasingDoubleKeyFrame.Value = -17.5;
myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);
myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(1);
myEasingDoubleKeyFrame.Value = 165.5;
myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);
myStoryboard.Children.Add(myDoubleAnimationUsingKeyFrames);
myStoryboard.Begin(myRectangle);
}
我猜问题出在Storyboard.SetTargetProperty
最佳答案
我用这条线解决了我的问题。
new PropertyPath("RenderTransform.Children[0].ScaleY")
谢谢大家。
关于c# - 如何在代码后面设置Storyboard.SetTargetProperty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9727133/