如何在XAML中为资源设置动画?

时间:2009-06-26 13:15:57

标签: wpf xaml animation resources storyboard

在XAML文档中,我有一个渐变画笔作为资源和一堆使用此资源的形状。我想使用故事板为画笔设置动画,但我不知道如何将资源中的画笔设置为故事板的目标。仅使用其名称不起作用,{StaticResource name}也不起作用。它甚至可能吗?

我更喜欢只有XAML的解决方案,但如果不能解决问题,我将使用代码隐藏。如果它让我离开Storyboard.Target和Storyboard.TargetProperty未分配。

编辑:我想为画笔的渐变停止设置动画。问题是,当它不是资源时,我可以轻松地为它设置动画,但是直接应用于对象。我可以点击Expression Blend来做到这一点。我只是不知道如何在它的资源(即在下面的代码中放置??而不是??为故事板为矩形创建)中设置动画。

code:
<UserControl.Resources>
    <LinearGradientBrush x:Key="Outline" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#7F7CE3FF" Offset="0"/>
        <GradientStop Color="#7F047695" Offset="1"/>
        <GradientStop Color="#FFFFFFFF" Offset="0.942"/>
    </LinearGradientBrush>
    <Storyboard x:Key="Glitter">
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="??" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Offset)">
            <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
            <SplineDoubleKeyFrame KeyTime="00:00:02.6000000" Value="0.529"/>
        </DoubleAnimationUsingKeyFrames>

    </Storyboard>
 ...

3 个答案:

答案 0 :(得分:3)

当您直接为背景/填充属性设置动画时,使用要为Storyboard.TargetName设置动画的对象名称(例如矩形),它会起作用:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Grid.Resources>
        <LinearGradientBrush x:Key="Outline" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#7F7CE3FF" Offset="0"/>
            <GradientStop Color="#7F047695" Offset="1"/>
            <GradientStop Color="#FFFFFFFF" Offset="0.942"/>
        </LinearGradientBrush>
    </Grid.Resources>

    <Border Name="border" 
            Background="{StaticResource Outline}"
            Width="200" Height="200" />
</Grid>

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
                                               Storyboard.TargetName="border" 
                                               Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[0].(GradientStop.Offset)">
                    <SplineDoubleKeyFrame KeyTime="00:00:0" Value="0"/>
                    <SplineDoubleKeyFrame KeyTime="00:00:1" Value="1"/>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>

修改

从背后的代码看来它似乎完全有可能:

XAML:

<Grid Name="grid">
    <Grid.Resources>
        <LinearGradientBrush x:Key="Outline" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#7F7CE3FF" Offset="0"/>
            <GradientStop Color="#7F047695" Offset="1"/>
            <GradientStop Color="#FFFFFFFF" Offset="0.942"/>
        </LinearGradientBrush>
    </Grid.Resources>

    <Border Background="{StaticResource Outline}"
            Width="100" Height="100" HorizontalAlignment="Left" />

    <Border Background="{StaticResource Outline}"
            Width="100" Height="100" HorizontalAlignment="Right" />
</Grid>

C#代码背后:

        LinearGradientBrush b = grid.Resources["Outline"] as LinearGradientBrush;

        b.GradientStops[0].BeginAnimation(GradientStop.OffsetProperty, new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1))));

答案 1 :(得分:2)

您无法为Brush类型的属性设置动画,您只能为具有适当动画类的类型设置动画,例如DoubleAnimation,PointAnimation或ColorAnimation(请注意,最后一个动画类型为Color的属性,而不是Brush)。

但是,一些Brushes具有double类型的DependencyProperties,您可以设置动画,例如LinearGradientBrush-Class的StartPoint和EndPoint属性。

如果你能详细说明动画应该做什么,也许我们可以找到一种解决方法。

编辑: 要为画笔设置动画,必须在动画触发器的范围内声明它,例如在Data-或ControlTemplate中。通过密钥动画资源不起作用。

答案 2 :(得分:0)

不确定您正在尝试在画笔内部制作动画,但动画画笔资源可能非常棘手。我没有时间输出所有内容,但这里有一些关于如何处理它的“教程”:

Animating Brushes with ObjectAnimationUsingKeyFrames