2

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

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 的限制或错误,还是我做错了什么?

谢谢

4

2 回答 2

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();
    }

享受!

于 2016-03-03T14:50:14.600 回答
-2

我不确定你如何让它在 WPF 中工作。根据http://msdn.microsoft.com/en-us/library/system.windows.media.animation.storyboard.targetproperty,目标属性必须是依赖属性。根据http://msdn.microsoft.com/en-us/library/system.windows.controls.border, Width 不是依赖属性。

于 2012-07-27T21:20:16.493 回答