I spent studying WPF last week, so this is still very much for me. One of the things I'm working on is simple animations. In this case, the bouncing smiley face.
My attack plan:
- Make a smiley. I have done it.
- Design bouncing animations on a simple object. I have done it.
- Abstract animation, so it can be used in several places (emoticon elements). I'm stuck here.
- Apply an abstract animation style to all elements of emoticons.
After step # 2, I had the following working XAML:
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="Test Window" Height="350" Width="620"> <Grid> <Canvas Margin="0,180,0,0"> <Ellipse Canvas.Left="10" Canvas.Top="10" Width="100" Height="100" Stroke="Blue" StrokeThickness="4" Fill="Aqua" /> <Ellipse Canvas.Left="30" Canvas.Top="12" Width="60" Height="30"> <Ellipse.Fill> <LinearGradientBrush StartPoint="0.45,0" EndPoint="0.5, 0.9"> <GradientStop Offset="0.2" Color="DarkMagenta" /> <GradientStop Offset="0.7" Color="Transparent" /> </LinearGradientBrush> </Ellipse.Fill> </Ellipse> <Ellipse Canvas.Left="33" Canvas.Top="35" Width="20" Height="20" Stroke="Blue" StrokeThickness="3" Fill="White" /> <Ellipse Canvas.Left="40" Canvas.Top="43" Width="6" Height="5" Fill="Black" /> <Ellipse Canvas.Left="68" Canvas.Top="35" Width="20" Height="20" Stroke="Blue" StrokeThickness="3" Fill="White" /> <Ellipse Canvas.Left="75" Canvas.Top="43" Width="6" Height="5" Fill="Black" /> <Path Name="mouth" Stroke="Blue" StrokeThickness="4" Data="M 35,75 Q 55,90 80,75 " /> </Canvas> <Grid Margin="100,5,0,0" Width="75" Height="300"> <Canvas> <Ellipse Height="40" Width="40" x:Name="theBall" Canvas.Left="16"> <Ellipse.Fill> <RadialGradientBrush GradientOrigin="0.75,0.25"> <GradientStop Color="Yellow" Offset="0.0" /> <GradientStop Color="Red" Offset="1.0" /> </RadialGradientBrush> </Ellipse.Fill> <Ellipse.RenderTransform> <TransformGroup> <ScaleTransform x:Name="aniSquash"/> <TranslateTransform x:Name="aniBounce"/> </TransformGroup> </Ellipse.RenderTransform> <Ellipse.Triggers> <EventTrigger RoutedEvent="Loaded"> <BeginStoryboard> <Storyboard SpeedRatio="2.0"> <DoubleAnimationUsingKeyFrames Duration="0:0:4.5" Storyboard.TargetName="aniBounce" Storyboard.TargetProperty="Y" RepeatBehavior="Forever"> <LinearDoubleKeyFrame Value="120" KeyTime="0:0:0"/> <SplineDoubleKeyFrame Value="260" KeyTime="0:0:2.2" KeySpline="0, 0, 0.5, 0"/> <LinearDoubleKeyFrame Value="260" KeyTime="0:0:2.25"/> <SplineDoubleKeyFrame Value="120" KeyTime="0:0:4.5" KeySpline="0, 0, 0, 0.5"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Duration="0:0:4.5" Storyboard.TargetName="aniSquash" Storyboard.TargetProperty="ScaleX" RepeatBehavior="Forever"> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:0"/> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:2"/> <LinearDoubleKeyFrame Value="1.3" KeyTime="0:0:2.25"/> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:2.5"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Duration="0:0:4.5" Storyboard.TargetName="aniSquash" Storyboard.TargetProperty="ScaleY" RepeatBehavior="Forever"> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:0"/> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:2"/> <LinearDoubleKeyFrame Value="0.7" KeyTime="0:0:2.25"/> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:2.5"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Ellipse.Triggers> </Ellipse> <Rectangle Height="5" Canvas.Left="10" Canvas.Top="285" Width="55" Fill="Black"/> </Canvas> </Grid> </Grid> </Window>
When changing the above, while working, XAML for step number 3, I introduced an error that I really do not understand. Here is the modified XAML that does not work:
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="Test Window" Height="350" Width="620"> <Window.Resources> <TransformGroup x:Key="aniBounceAndSquash"> <ScaleTransform x:Name="aniSquash"/> <TranslateTransform x:Name="aniBounce"/> </TransformGroup> <Style x:Key="styleBounceAndSquash" TargetType="FrameworkElement"> <Setter Property="RenderTransform" Value="{StaticResource aniBounceAndSquash}" /> <Style.Triggers> <EventTrigger RoutedEvent="Loaded"> <BeginStoryboard> <Storyboard SpeedRatio="2.0"> <DoubleAnimationUsingKeyFrames Duration="0:0:4.5" Storyboard.TargetName="aniBounce" Storyboard.TargetProperty="Y" RepeatBehavior="Forever"> <LinearDoubleKeyFrame Value="120" KeyTime="0:0:0"/> <SplineDoubleKeyFrame Value="260" KeyTime="0:0:2.2" KeySpline="0, 0, 0.5, 0"/> <LinearDoubleKeyFrame Value="260" KeyTime="0:0:2.25"/> <SplineDoubleKeyFrame Value="120" KeyTime="0:0:4.5" KeySpline="0, 0, 0, 0.5"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Duration="0:0:4.5" Storyboard.TargetName="aniSquash" Storyboard.TargetProperty="ScaleX" RepeatBehavior="Forever"> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:0"/> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:2"/> <LinearDoubleKeyFrame Value="1.3" KeyTime="0:0:2.25"/> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:2.5"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Duration="0:0:4.5" Storyboard.TargetName="aniSquash" Storyboard.TargetProperty="ScaleY" RepeatBehavior="Forever"> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:0"/> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:2"/> <LinearDoubleKeyFrame Value="0.7" KeyTime="0:0:2.25"/> <LinearDoubleKeyFrame Value="1" KeyTime="0:0:2.5"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <Canvas Margin="0,180,0,0"> <Ellipse Canvas.Left="10" Canvas.Top="10" Width="100" Height="100" Stroke="Blue" StrokeThickness="4" Fill="Aqua" /> <Ellipse Canvas.Left="30" Canvas.Top="12" Width="60" Height="30"> <Ellipse.Fill> <LinearGradientBrush StartPoint="0.45,0" EndPoint="0.5, 0.9"> <GradientStop Offset="0.2" Color="DarkMagenta" /> <GradientStop Offset="0.7" Color="Transparent" /> </LinearGradientBrush> </Ellipse.Fill> </Ellipse> <Ellipse Canvas.Left="33" Canvas.Top="35" Width="20" Height="20" Stroke="Blue" StrokeThickness="3" Fill="White" /> <Ellipse Canvas.Left="40" Canvas.Top="43" Width="6" Height="5" Fill="Black" /> <Ellipse Canvas.Left="68" Canvas.Top="35" Width="20" Height="20" Stroke="Blue" StrokeThickness="3" Fill="White" /> <Ellipse Canvas.Left="75" Canvas.Top="43" Width="6" Height="5" Fill="Black" /> <Path Name="mouth" Stroke="Blue" StrokeThickness="4" Data="M 35,75 Q 55,90 80,75 " /> </Canvas> <Grid Margin="100,5,0,0" Width="75" Height="300"> <Canvas> <Ellipse Height="40" Width="40" x:Name="theBall" Canvas.Left="16" Style="{StaticResource styleBounceAndSquash}"> <Ellipse.Fill> <RadialGradientBrush GradientOrigin="0.75,0.25"> <GradientStop Color="Yellow" Offset="0.0" /> <GradientStop Color="Red" Offset="1.0" /> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> <Rectangle Height="5" Canvas.Left="10" Canvas.Top="285" Width="55" Fill="Black"/> </Canvas> </Grid> </Grid> </Window>
Error "TargetName property could not be set in Style Setter. Line 20 Position 79."
If I canβt install it in style, how to install it?