2009-12-05 36 views
3

这是XAML一个简单的WPF窗口: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> 

这样我就可以在窗口构造做到这一点:

 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> 

回答

4

你没有任何名为“(UIElement)”的东西,所以TargetName无法解析。 Storyboard.Begin(FrameworkElement)的文档说“没有TargetName的动画应用于包含对象”,所以你应该能够离开TargetName,并将动画应用到Background.GradientStops [1]。或者,为了避免需要代码隐藏,为什么不在你的Style中使用EventTrigger来运行Storyboard?为什么不在你的Style中使用EventTrigger?有关示例,请参阅MSDN中的EventTrigger文档。

+0

这两个建议的工作,谢谢 – Anthony

相关问题