這是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.TargetName
和Storyboard.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>
這兩個建議的工作,謝謝 – Anthony