【问题标题】:How to animate Windows Phone 8.1 AppBar background color?如何为 Windows Phone 8.1 AppBar 背景颜色设置动画?
【发布时间】:2014-09-09 16:49:51
【问题描述】:

我似乎无法使用ColorAnimationAppBar 的背景颜色设置为动画。

<Storyboard x:Name="FadeColorStoryboard">
    <ColorAnimation
        Storyboard.TargetName="bar"
        Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
        From="Yellow" To="Green" Duration="0:0:1" />
</Storyboard>

我已经通过为TextBlock 的前景色设置动画来测试情节提要,它可以工作。我有任何可以为 AppBar 的背景颜色设置动画的方法吗?

我也尝试使用自定义附加属性来更改背景颜色,但我无法将情节提要的 TargetProperty 设置为自定义附加属性,这与 Silverlight 不同。

【问题讨论】:

  • 我要研究的第一件事是在动画上将EnableDependentAnimation 属性设置为true...
  • @FilipSkakun 忘了说我已经试过了,但没用。
  • 好吧,我现在没有时间检查它,但至少在 Windows Phone Silverlight 世界中,应用栏属性不可绑定且可能不可动画化,因此针对附加财产可能是一种解决方案。您可能无法使用 XAML 来定位它,但也许您可以在后面的代码中实现。我现在不记得了。如果不是附属财产,则为常规财产。我曾经使用过的最奇怪的解决方法是拥有一个不可见的Slider 控件,为其Value 属性设置动画并将更改转发到其他地方(用于更改ScrollViewer 偏移量)。
  • @FilipSkakun 我也尝试在代码中定位附加属性,但无济于事。根据this SO question 的说法,这是可能的,但仅适用于 Silverlight 应用程序。

标签: xaml animation windows-runtime winrt-xaml windows-phone-8.1


【解决方案1】:

好的,所以我想出了一个解决方案。这不是最好的解决方案,但它可以满足我的需求。如果有更好的方法,我仍然会感兴趣,但现在就可以了。

编辑:忘了提一下,这需要 ColorAnimation 上的 EnableDependentAnimation 出于某种原因设置为 true。

我将CommandBar 子类化并添加了我自己的BackgroundColor 可以动画的属性。

class CommandBarEx : CommandBar
{
    public Color BackgroundColor
    {
        get { return (Color)GetValue(BackgroundColorProperty); }
        set { SetValue(BackgroundColorProperty, value); }
    }

    public static readonly DependencyProperty BackgroundColorProperty =
        DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(CommandBarEx), new PropertyMetadata(Colors.Gray, onBackgroundColorPropertyChanged));

    static void onBackgroundColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var bar = d as CommandBarEx;
        if (bar == null)
            return;

        var brush = bar.Background as SolidColorBrush;
        if (brush == null)
        {
            bar.Background = new SolidColorBrush((Color)e.NewValue);
        }
        else
        {
            brush.Color = (Color)e.NewValue;
            bar.Background = brush;  // force property change event
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多