动画边框控件的宽度

时间:2012-07-27 20:41:14

标签: c# microsoft-metro windows-runtime winrt-xaml

我正在尝试在Windows 8 Metro应用中为Border控件的宽度设置动画。我设法在边框上设置其他属性的动画,例如不透明度:

private void Item_Tapped(object sender, RoutedEventArgs e)
{
    var border = sender as Border;

    var animation = new DoubleAnimation();
    animation.To = 0.0;
    animation.Duration = new Duration(TimeSpan.FromSeconds(1));

    var storyboard = new Storyboard();
    storyboard.Children.Add(animation);

    Storyboard.SetTarget(animation, border);
    Storyboard.SetTargetProperty(animation, "Opacity");

    storyboard.Begin();
}

但是当我尝试为控件的宽度设置动画时:

private void Item_Tapped(object sender, RoutedEventArgs e)
{
    var border = sender as Border;

    var animation = new DoubleAnimation();
    animation.To = 600.0;
    animation.Duration = new Duration(TimeSpan.FromSeconds(1));

    var storyboard = new Storyboard();
    storyboard.Children.Add(animation);

    Storyboard.SetTarget(animation, border);
    Storyboard.SetTargetProperty(animation, "Width");

    storyboard.Begin();
}

什么都没发生。

我可以在WPF中执行此操作,但不能在Windows 8中执行此操作。这是Metro的限制或错误,还是我做错了什么?

由于

2 个答案:

答案 0 :(得分:0)

我可以通过启用EnableDependentAnimation

来使其工作
    public static async Task Width(FrameworkElement obj, double doubleValue, int milliseconds)
    {
        var animation= new DoubleAnimation
        {
            To = doubleValue,
            Duration = TimeSpan.FromMilliseconds(milliseconds),
            EnableDependentAnimation = true// <== here
        };

        Storyboard.SetTarget(animation, obj);
        Storyboard.SetTargetProperty(animation, "Width");

        var sb = new Storyboard();
        sb.Children.Add(animation);
        await sb.BeginAsync();
    }

享受!

答案 1 :(得分:-2)

我不确定你是如何在WPF中工作的。根据{{​​3}},target属性必须是依赖属性。根据{{​​3}},Width不是依赖属性。