2009-12-05 40 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

相關問題