5

在 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>
 ...
4

3 回答 3

4

当您直接为背景/填充属性设置动画时,它会起作用,使用您想要动画的对象(例如矩形)的名称作为 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))));
于 2009-06-26T13:59:26.257 回答
1

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

但是,某些画笔具有 double 类型的 DependencyProperties,您可以对其进行动画处理,例如 LinearGradientBrush-Class 的 StartPoint 和 EndPoint 属性。

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

编辑:要为画笔设置动画,必须在动画触发器的范围内声明,例如在数据或 ControlTemplate 中。通过其键为资源设置动画将不起作用。

于 2009-06-26T13:29:54.803 回答
0

不确定您要在画笔内部设置动画的确切内容,但是为画笔资源设置动画可能非常棘手。我没有时间输入所有内容,但这里有一些关于如何处理它的“教程”:

使用 ObjectAnimationUsingKeyFrames 动画画笔

于 2009-06-26T13:32:54.250 回答