gpt4 book ai didi

c# - 新手 WPF 动画 : How to begin the Animation?

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

我已经创建了一个自定义动画类,遵循了这个指南 WPF Tutorial - Part 2 :

public class EarthAnimation : AnimationTimeline
{
private Earth _earth = new Earth(250);

public static readonly DependencyProperty FromProperty =
DependencyProperty.Register("From", typeof (int), typeof (EarthAnimation),
new PropertyMetadata(default(int)));

public int From
{
get { return (int) GetValue(FromProperty); }
set { SetValue(FromProperty, value); }
}

public static readonly DependencyProperty ToProperty =
DependencyProperty.Register("To", typeof (int), typeof (EarthAnimation), new PropertyMetadata(default(int)));

public int To
{
get { return (int) GetValue(ToProperty); }
set { SetValue(ToProperty, value); }
}

protected override Freezable CreateInstanceCore()
{
return new EarthAnimation();
}

public override Type TargetPropertyType
{
get { return typeof (int); }
}

public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue,
AnimationClock animationClock)
{
Vector fromVal = ((Earth) GetValue(EarthAnimation.FromProperty)).Coords;
Vector toVal = ((Earth) GetValue(EarthAnimation.ToProperty)).Coords;


return new DenseVector(new[]
{
_earth.DistanceToStar*Math.Cos(_earth.WE*animationClock.CurrentProgress.Value),
_earth.BE*Math.Sin(_earth.WE*animationClock.CurrentProgress.Value)
});
}
}

我的 Xaml 看这个(简化):

<Grid Name="MyCanvasGrid"
Width="600"
Height="400"
Background="Transparent"
RenderTransformOrigin="0.5,0.5">

<Canvas x:Name="CanvasDrawingArea"
Grid.Row="0"
Width="Auto"
Height="Auto"
HorizontalAlignment="Center"
VerticalAlignment="Center"
ClipToBounds="False"
RenderTransformOrigin="0.5,0.5"
RenderTransform="1 0 0 -1 0 0">
</Canvas>
</Grid>

我想在按下 PlayBt 时开始我的动画(在代码中)。

private void PlayBt_OnClick(object sender, RoutedEventArgs e)
{
DrawEarth();
var dbAnim = new EarthAnimation();

dbAnim.Duration = new Duration(TimeSpan.FromSeconds(4));
dbAnim.RepeatBehavior = RepeatBehavior.Forever;

// Set the start value and end value.
dbAnim.From = 1;
dbAnim.To = 3660;
}

如何在代码中开始动画?

最佳答案

要启动动画,您需要使用“Begin()”函数启动动画;

dbAnim.Begin();

关于c# - 新手 WPF 动画 : How to begin the Animation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21022372/

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