WPF:我可以在样式中放置颜色动画吗?

4
这是一个简单的WPF窗口XAML代码:

<Window x:Class="AnimateTest.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"
        x:Name="MainWindow"
        Style="{StaticResource TestStyle}">
    <Grid>
    </Grid>
</Window>

请注意它有一个样式。我们可以用这个样式做什么?这是 App.xaml,它给它一个浅蓝色的背景。
<Application x:Class="AnimateTest.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <Style x:Key="TestStyle">
            <Setter Property="Window.Background" Value="AliceBlue" />
        </Style>
    </Application.Resources>
</Application>

为了更加复杂,这是为其提供蓝色渐变背景的背景:
<Application x:Class="AnimateTest.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <LinearGradientBrush x:Key="BackgroundBrush"
            EndPoint="0.6,0.6" StartPoint="0,0">
            <GradientStop Color="#FFFFFFFF" Offset="0" />
            <GradientStop Color="#FFD0D0F0" Offset="1" />
        </LinearGradientBrush>
        <Style x:Key="TestStyle">
            <Setter Property="Window.Background" Value="{StaticResource BackgroundBrush}" />
        </Style>
    </Application.Resources>
</Application>

我要做的最后一步是将这种颜色进行动画处理。我已经...
<Application x:Class="AnimateTest.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <LinearGradientBrush x:Key="BackgroundBrush"
            EndPoint="0.6,0.6" StartPoint="0,0">
            <GradientStop Color="#FFFFFFFF" Offset="0" />
            <GradientStop Color="#FFD0D0F0" Offset="1" />
        </LinearGradientBrush>
        <Style x:Key="TestStyle">
            <Setter Property="Window.Background" Value="{StaticResource BackgroundBrush}" />
        </Style>
        <Storyboard x:Key="ThemeAnimation">
            <ColorAnimationUsingKeyFrames
            Storyboard.TargetName="(UIElement)"
            Storyboard.TargetProperty="Background.GradientStops[1].Color"
            Duration="0:0:10"
            RepeatBehavior="Forever">
                <ColorAnimationUsingKeyFrames.KeyFrames>
                    <LinearColorKeyFrame Value="#FFD0D0F0" KeyTime="0:0:0" />
                    <LinearColorKeyFrame Value="#FFF0D0F0" KeyTime="0:0:10" />
                </ColorAnimationUsingKeyFrames.KeyFrames>
            </ColorAnimationUsingKeyFrames>
        </Storyboard>
    </Application.Resources>
</Application>

所以我可以在Window构造函数中实现这个功能:

        object themeAnimationObject = this.FindResource("ThemeAnimation");
        Storyboard themeAnimation = themeAnimationObject as Storyboard;
        themeAnimation.Begin(this);

但是我收到了一个异常:
(UIElement)' name cannot be found in the name scope of 'AnimateTest.Window1'

我尝试了各种 Storyboard.TargetNameStoryboard.TargetProperty 属性的值组合,但都没有成功,我就像在黑暗中摸索。最好的结果是能够将样式、动画和所有内容应用于任何窗口,而不需要或只需要很少的 C# 代码。
更新:这里是基于 itowlson 的答案的可工作的 App.xaml 文件。
<Application x:Class="AnimateTest.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <LinearGradientBrush x:Key="BackgroundBrush"
            EndPoint="0.6,0.6" StartPoint="0,0">
            <GradientStop Color="#FFFFFFFF" Offset="0" />
            <GradientStop Color="#FFD0D0F0" Offset="1" />
        </LinearGradientBrush>
        <Style x:Key="TestStyle" TargetType="FrameworkElement">
            <Setter Property="Window.Background" Value="{StaticResource BackgroundBrush}" />
            <Style.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames
                                Storyboard.TargetProperty="Background.GradientStops[1].Color"
                                Duration="0:0:10"
                                RepeatBehavior="Forever"
                                AutoReverse="True">
                                <ColorAnimationUsingKeyFrames.KeyFrames>
                                    <LinearColorKeyFrame Value="#FFD0D0F0" KeyTime="0:0:0" />
                                    <LinearColorKeyFrame Value="#FFF0D0F0" KeyTime="0:0:10" />
                                </ColorAnimationUsingKeyFrames.KeyFrames>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Application.Resources>
</Application>
1个回答

4
您没有任何名为“(UIElement)”的内容,因此TargetName无法解析。Storyboard.Begin(FrameworkElement)文档中说:“没有TargetName的动画应用于containingObject”,因此您可以只省略TargetName,动画将应用于传递给它的Window的Background.GradientStops [1] .Color。
另外,为了避免需要编写代码,为什么不在样式中使用EventTrigger来运行Storyboard?请参阅MSDN中的EventTrigger文档以获取示例。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接