【问题标题】:WPF: Can I put a colour animation in a style?WPF:我可以在样式中放置彩色动画吗?
【发布时间】:2010-12-23 14:49:24
【问题描述】:

这是一个简单的 XAML 中的 WPF 窗口:

<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>

【问题讨论】:

    标签: wpf xaml animation colors styles


    【解决方案1】:

    您没有任何名为“(UIElement)”的内容,因此 TargetName 无法解析。 Storyboard.Begin(FrameworkElement) 的文档说“没有 TargetName 的动画被应用到 containingObject”,所以你应该可以不用 TargetName,动画将被应用到背景。 GradientStops[1].你传入的窗口的颜色。

    另外,为了避免代码隐藏的需要,为什么不在您的样式中使用 EventTrigger 来运行情节提要?有关示例,请参阅 MSDN 中的 EventTrigger 文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-01
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      相关资源
      最近更新 更多