WP8.1:从当前位置开始动画
本文关键字:位置 开始 动画 WP8 | 更新日期: 2023-09-27 18:23:40
我希望控件基于另一个属性更改其位置,但将动画设置为新位置。但是,每当我启动新动画时,情节提要都会将控件的位置重置为XAML中设置的属性。
我在做什么:
var animation = new DoubleAnimation();
animation.Duration = TimeSpan.FromSeconds(0.1);
// This gets the value that was set in the XAML, not the current value
// animation.From = Canvas.GetLeft(BackgroundImage);
animation.To = newLocation;
Storyboard.SetTarget(animation, BackgroundImage);
Storyboard.SetTargetProperty(animation, "(Canvas.Left)");
Storyboard story = new Storyboard();
story.Children.Add(animation);
story.Begin();
是否有任何方法可以从控件获取当前动画值以设置为动画。从,或者有没有方法告诉情节提要从当前属性的位置开始?
假设如果要设置Canvas.Left
属性的动画,则From
的值实际上是
Canvas.GetLeft(BackgroundImage)
但是,通过未指定From
属性(如注释方式),Storyboard
应该从当前位置开始。所以你并不真的需要上面的代码。
你的动画在我看来很好。可能是其他原因造成了问题。
更新
下面是一个工作示例,每当属性发生更改时,控件都会被设置到相应的位置。
在我的xaml中,我创建了一个Slider
控件,每次更改Slider
的Value
时,我都想将我的BackgroundImage
控件的Canvas.Left
动画化为该Value
。这有点像你在问题中提到的另一处房产。
Xaml
<Slider x:Name="MySlider" Margin="12,0" VerticalAlignment="Bottom" Minimum="20" Maximum="220" SmallChange="10" Value="20" />
代码
private Storyboard _storyboard = new Storyboard();
private DoubleAnimation _animation = new DoubleAnimation { Duration = TimeSpan.FromSeconds(0.1) };
public MainPage()
{
this.InitializeComponent();
Storyboard.SetTarget(_animation, this.BackgroundImage);
Storyboard.SetTargetProperty(_animation, "(Canvas.Left)");
_storyboard.Children.Add(_animation);
this.MySlider.ValueChanged += (s, e) =>
{
_animation.To = this.MySlider.Value;
_storyboard.Begin();
};
}
正如您所看到的,我为Storyboard
和DoubleAnimation
实例保留了两个局部变量。然后,当调用ValueChanged
时,只需设置To
并启动动画。