est simple fenêtre WPF en XAML:WPF: Puis-je mettre une animation couleur dans un style? Ce
<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>
Notez que est un style. Que pouvons-nous faire avec le style? C'est le App.xaml
qui lui donne un fond bleu clair
<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>
Pour obtenir plus complexe, c'est l'arrière-plan qui lui donne un fond bleu dégradé:
<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>
La dernière étape que je veux faire est d'animer cette couleur. Je
<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>
donc je peux le faire dans le constructeur de fenêtre:
object themeAnimationObject = this.FindResource("ThemeAnimation");
Storyboard themeAnimation = themeAnimationObject as Storyboard;
themeAnimation.Begin(this);
Mais je reçois une exception:
(UIElement)' name cannot be found in the name scope of 'AnimateTest.Window1'
J'ai essayé différentes combinaisons de valeurs pour Storyboard.TargetName
de l'animation et Storyboard.TargetProperty
propriétés, mais ils n'ont pas fonctionné, je suis juste à tâtons dans le noir. Le meilleur résultat serait en mesure d'appliquer le style, des animations et tout à une fenêtre sans ou avec un minimum C# code
Mise à jour: Voici le App.xaml de travail basée sur la réponse de itowlson:
<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>
Les deux suggestions fonctionnent, merci – Anthony