为什么Storyboard.SetTargetName可以正常工作,但Storyboard.SetTarget却没有?在这里xaml -
<Grid Grid.Row="0" ClipToBounds="True">
<X:SmartContentControl x:Name="smartContent" Content="{Binding Path=MainContent}" ContentChanging="smartContent_ContentChanging">
<X:SmartContentControl.RenderTransform>
<TranslateTransform x:Name="translateTransformNew" X="0" Y="0"/>
</X:SmartContentControl.RenderTransform>
</X:SmartContentControl>
<ContentControl Content="{Binding ElementName=smartContent, Path=LastImage}">
<ContentControl.RenderTransform>
<TranslateTransform x:Name="translateTransformLast" X="0" Y="0"/>
</ContentControl.RenderTransform>
</ContentControl>
</Grid>
Run Code Online (Sandbox Code Playgroud)
在这里C#
private void smartContent_ContentChanging(object sender, RoutedEventArgs e)
{
Storyboard storyBoard = new Storyboard();
DoubleAnimation doubleAnimation1 = new DoubleAnimation(0.0, -smartContent.RenderSize.Width, new Duration(new TimeSpan(0, 0, 0, 0, 500)));
DoubleAnimation doubleAnimation2 = new DoubleAnimation(smartContent.RenderSize.Width, 0.0, new Duration(new TimeSpan(0, 0, 0, 0, 500)));
doubleAnimation1.AccelerationRatio = 0.5;
doubleAnimation2.DecelerationRatio = 0.5;
storyBoard.Children.Add(doubleAnimation1);
storyBoard.Children.Add(doubleAnimation2);
Storyboard.SetTarget(doubleAnimation1, this.translateTransformLast); //--- this …Run Code Online (Sandbox Code Playgroud) 任何人都可以帮我试图找出为什么这不起作用.
Brush变量包含一个预先填充的画笔列表.如果我尝试BeginAnimation在迭代期间直接应用它,它可以正常工作.但是每个动画分别开始有很大的开销......
所以我试图将所有动画放在一个故事板中,然后立即将它们全部解开......
var storyBoard = new Storyboard();
var duration = new Duration(TimeSpan.FromMilliseconds(time));
foreach (Brush brush in brushes)
{
var animation = new DoubleAnimation(toValue, duration);
storyBoard.Children.Add(animation);
Storyboard.SetTargetProperty(animation, new PropertyPath(Brush.OpacityProperty));
Storyboard.SetTarget(animation, brush);
}
storyBoard.Begin();
Run Code Online (Sandbox Code Playgroud)
这段代码什么都不做(我可以看到......).
谢谢!!
编辑:仍然不确定SetTarget方法有什么问题,要么是一个bug,要么我只是不应该使用它.无论如何,我解决了在运行时为我的画笔生成唯一名称并使用SetTargetName方法的问题.
再次感谢所有的建议.
我有我的自定义 3D 模型类 (Model),它包含 Visual3D 元素和一个Storyboard(sb) 来保存与该模型相关的动画。我正在尝试使用 旋转 Visual3D 元素,Storyboard但不幸的是它不起作用。
这是代码片段
public void AnimationRotate(Model model, double duration, double startTime, RepeatBehavior behaviour)
{
//Rotate transform 3D
RotateTransform3D rotateTransform = new RotateTransform3D();
//assign transform to the model
model.Visual3D.Transform = Transform3DHelper.CombineTransform(model.Visual3D.Transform, rotateTransform);
//define the rotation axis
AxisAngleRotation3D rotateAxis = new AxisAngleRotation3D(new Vector3D(0, 0, 1), 180);
//create 3D rotation animation
Rotation3DAnimation rotateAnimation = new Rotation3DAnimation(rotateAxis, TimeSpan.FromSeconds(0.5));
//rotation behaviour
rotateAnimation.RepeatBehavior = behaviour;
//start animation from time
rotateAnimation.BeginTime = TimeSpan.FromSeconds(startTime);
//begin animation …Run Code Online (Sandbox Code Playgroud)