gpt4 book ai didi

c# - 来自代码的 Wpf 动画

转载 作者:太空宇宙 更新时间:2023-11-03 16:39:57 25 4
gpt4 key购买 nike

有一些类似的线程,但我没有找到解决问题的方法。这是我在这里的第一篇文章。
事情是这样的:

Viewport3D viewPort3D;

GeometryModel3D geometryModel = new GeometryModel3D();
Transform3DGroup transform3DGroup = new Transform3DGroup();

...

// Rotation
RotateTransform3D rotateTransform3D = new RotateTransform3D();
AxisAngleRotation3D axisAngleRotation3d = new AxisAngleRotation3D();
axisAngleRotation3d.Axis = new Vector3D(0, 1, 0);
axisAngleRotation3d.Angle = angle;
rotateTransform3D.Rotation = axisAngleRotation3d;
transform3DGroup.Children.Add(rotateTransform3D);

// Translation
TranslateTransform3D translateTransform3D = new TranslateTransform3D();
translateTransform3D.OffsetX = offsetX;
transform3DGroup.Children.Add(translateTransform3D);

// Adding transforms
geometryModel.Transform = transform3DGroup;

Model3DGroup model3DGroup = new Model3DGroup();
model3DGroup.Children.Add( image.getGeometryModel3D() );

modelVisual3D.Content = model3DGroup;
viewPort3D.Children.Add( modelVisual3D );

现在我想使用 Storyboard进行翻译(因为稍后我还想向该 Storyboard添加旋转):

Storyboard s = new Storyboard();

Transform3DGroup transform3DGroup = model3DGroup.Children.ElementAt(current).Transform as Transform3DGroup;

for (int j = 0; j < transform3DGroup.Children.Count; ++j)
{
if (transform3DGroup.Children.ElementAt(j) is TranslateTransform3D)
{
TranslateTransform3D translation = transform3DGroup.Children.ElementAt(j) as TranslateTransform3D;

DoubleAnimation doubleAnimation = new DoubleAnimation();
doubleAnimation.From = 0;
doubleAnimation.To = 2;
doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
doubleAnimation.AutoReverse = true;
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;

s.Children.Add(doubleAnimation);
s.Duration = new Duration(TimeSpan.FromSeconds(1));

Storyboard.SetTarget(doubleAnimation, model3DGroup.Children.ElementAt(current));
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Model3D.Transform).(Transform3DGroup.Children)[1].(TranslateTransform3D.OffsetX)"));

s.Begin(); // Exception during the execution.
}
}

最后一行异常:

'[Unknown]' property value in the path '(Model3D.Transform).(Transform3DGroup.Children)[1].(TranslateTransform3D.OffsetX)' points to immutable instance of 'System.Windows.Media.Media3D.TranslateTransform3D'.

我取的 PropertyPath 类似于 blend 4 中生成的路径。

感谢您的帮助。

最佳答案

我认为因为 translate tranform 3d 是一个不可变的实例,所以必须指出它在进行渲染/转换时应该是可变的。

我猜

  1. 我们可以向不可变的 TranslateTransform3D 对象提供 x:Name,使其可变。

  2. 绑定(bind)到它的属性而不是让它动起来。

例如在你的情况下

        NameScope.SetNameScope(this, new NameScope());
this.RegisterName("AxisRotation", MyAxisRotation3DObject.Rotation);
this.RegisterName("TranslateTransformation", MyTranslation3DObject);

通过这种方式,我们为 Axis Rotation 3D 和 Translate Transform 3D 对象命名,然后在双动画中将它们称为 Storyboard.SetTargetName(.., "AxisRotation")Storyboard.SetTargetName (.., "TranslateTransformation") 并访问它们的直接属性,例如 Storyboard.SetTargetProperty(.., new PropertyPath("Angle"))Storyboard.SetTargetProperty(. ., new PropertyPath("OffsetX")) 相应的

关于c# - 来自代码的 Wpf 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7953008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com