Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WP8.1:从当前位置启动动画_C#_Xaml_Animation_Windows Phone 8.1 - Fatal编程技术网

C# WP8.1:从当前位置启动动画

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

我想要一个控件基于另一个属性更改其位置,但要将其设置为该新位置的动画。但是,每当我启动新动画时,情节提要都会将控件的位置重置为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.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中设置的值。