C# WP8.1:从当前位置启动动画
我想要一个控件基于另一个属性更改其位置,但要将其设置为该新位置的动画。但是,每当我启动新动画时,情节提要都会将控件的位置重置为XAML中设置的属性 以下是我正在做的:C# WP8.1:从当前位置启动动画,c#,xaml,animation,windows-phone-8.1,C#,Xaml,Animation,Windows Phone 8.1,我想要一个控件基于另一个属性更改其位置,但要将其设置为该新位置的动画。但是,每当我启动新动画时,情节提要都会将控件的位置重置为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
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.GetLeft(BackgroundImage)
但是,如果将From
属性保留为未指定的属性(如您对它的注释),则情节提要应该从当前位置开始。所以你不需要上面的代码
我觉得你的动画不错。可能是其他原因导致了这个问题
更新
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();
};
}
下面是一个工作示例,在该示例中,每当属性发生更改时,控件都会被设置为相应位置的动画
在我的xaml中,我创建了一个滑块
控件,每次更改滑块
的值
时,我都想将画布的动画。我的背景图像
控件的左侧
设置为该值
。这有点像你在问题中提到的另一个属性
Xaml
<Slider x:Name="MySlider" Margin="12,0" VerticalAlignment="Bottom" Minimum="20" Maximum="220" SmallChange="10" Value="20" />
如您所见,我为情节提要
和双动画
实例保留了两个局部变量。然后,当调用ValueChanged
时,只需将设置为
并启动动画。当我进行测试时,Canvas.GetLeft总是返回XAML中设置的值,而不是故事板中设置的当前值。如果我启动一个新的故事板,它总是将左值重置为XAML中设置的值。