利用wpf写一个loading动画
时间: 2024-03-06 10:46:15 浏览: 131
以下是一个简单的WPF Loading动画的代码:
```xaml
<Window x:Class="LoadingAnimationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="LoadingAnimationWindow" Height="150" Width="350">
<Grid>
<Ellipse x:Name="Ellipse1" Width="30" Height="30" Fill="LightBlue">
<Ellipse.RenderTransform>
<TranslateTransform X="0" Y="0"/>
</Ellipse.RenderTransform>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)"
From="0" To="200" Duration="0:0:1" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
<Ellipse x:Name="Ellipse2" Width="30" Height="30" Fill="LightBlue">
<Ellipse.RenderTransform>
<TranslateTransform X="0" Y="0"/>
</Ellipse.RenderTransform>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)"
From="0" To="-200" Duration="0:0:1" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
</Grid>
</Window>
```
这个Loading动画使用了两个椭圆(Ellipse)来表示动画,每个椭圆都有一个 `TranslateTransform` 作为其渲染变换(Render Transform),并在其 `Loaded` 事件中启动一个 `Storyboard`,使用 `DoubleAnimation` 来改变 `TranslateTransform` 的 X 坐标值,从而实现椭圆的移动。两个椭圆的移动方向相反,从而形成动画效果。
阅读全文