Animation using WPF

The code for an animation would be difficult to predict without an example such as Beginner's WPF Animation Tutorial. ("Double" in DoubleAnimation refers to the type of number being changed and not to the number of times the animation is performed!) To try this example, start a new Oxygene for .Net WPF application named WPF_AnimationDemo and paste in the Oxygene code below to replace the default code of Window1.xaml.pas and the XAML code to replace the existing code of Window1.xaml.

The created window, shortly after clicking the expander and during its animation, appears as follows.

Animation in progress

Animation in progress

Oxygene code

namespace WPF_AnimationDemo;

interface

uses
  System.Windows, System.Windows.Controls,  System.Windows.Media,
  System.Windows.Media.Animation, System.Windows.Shapes;

type
  Window1 = public partial class(System.Windows.Window)
  private
    method infoExpander_Expanded(sender: System.Object; e: System.Windows.RoutedEventArgs);
    method rect_Loaded(sender: System.Object; e: System.Windows.RoutedEventArgs);
  public
    constructor;
  end;
  
implementation

constructor Window1;
begin
  InitializeComponent();
end;

method Window1.rect_Loaded(sender: System.Object; e: System.Windows.RoutedEventArgs);
begin
  var da: DoubleAnimation := new DoubleAnimation;
  da.From := -90;
  da.To := 270;
  da.Duration := new Duration(TimeSpan.FromSeconds(60));
  da.RepeatBehavior := RepeatBehavior.Forever;
  var rt: RotateTransform := new RotateTransform;
  rt.CenterX := 20;
  rt.CenterY := 4;
  rect.RenderTransform := rt; 
  rt.BeginAnimation(RotateTransform.AngleProperty, da);  
end;

method Window1.infoExpander_Expanded(sender: System.Object; e: System.Windows.RoutedEventArgs);
begin
  var da: DoubleAnimation := new DoubleAnimation;
  da.From := 40;
  da.To := 160;
  da.Duration := new Duration(TimeSpan.FromSeconds(3));
  infoExpander.BeginAnimation(MaxHeightProperty, da);
end;
 
end.

XAML code

<?xml version='1.0' encoding='utf-8' ?>
<Window x:Class="WPF_AnimationDemo.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="WPF Animation Demo" Height="300" Width="300">
  <Grid>
    <Rectangle x:Name="rect" Fill="#FF1010F3" HorizontalAlignment="Left"
                 Height="8" Margin="127,126,0,0" Stroke="#FF272A66" 
                 VerticalAlignment="Top" Width="131" RadiusX="4" RadiusY="4"
                 Loaded="rect_Loaded"/>
    <Expander Name="infoExpander" Background="White" HorizontalAlignment="Left"
              VerticalAlignment="Top" Header="Info" ExpandDirection="Down"
              IsExpanded="False" Width="100" MaxHeight="150"
              Expanded="infoExpander_Expanded">
      <TextBlock TextWrapping="Wrap">
        The second hand makes one revolution per minute. The pivot is a point  
        20 pixels from one end.    
      </TextBlock>
    </Expander>
  </Grid>
</Window>

Programming - a skill for life!

How to write programs in the Oxygene for .Net dialect of Pascal