SampleViewer.axaml代碼
<Window xmlns="https://github.com/avaloniaui" 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" Width="800" Height="600" xmlns:local="using:AvaloniaUI" x:Class="AvaloniaUI.SampleViewer" Title="SampleViewer"> <TabControl> <TabControl.Styles> <Style Selector="TabItem"> <Setter Property="FontSize" Value="17"/> <Setter Property="FontWeight" Value="Bold"/> </Style> </TabControl.Styles> <TabItem Header="顏色變化效果"> <local:SimpleExample/> </TabItem> <TabItem Header="跟隨效果"> <local:FollowExample/> </TabItem> <TabItem Header="可復(fù)用跟隨效果"> <local:ReusableFollowExample/> </TabItem> <TabItem Header="粒子效果"> <local:ParticleEffectExamples/> </TabItem> </TabControl> </Window>
SampleViewer.axaml.cs代碼
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace AvaloniaUI;
public partial class SampleViewer : Window
{
public SampleViewer()
{
InitializeComponent();
}
}
Tab - SimpleExample.axaml代碼
<UserControl xmlns="https://github.com/avaloniaui" 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" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="AvaloniaUI.SimpleExample"> <UserControl.Resources> <SolidColorBrush x:Key="AnimatedBrush"/> </UserControl.Resources> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Rectangle Width="50" Height="50" Fill="{StaticResource AnimatedBrush}"/> </StackPanel> </UserControl>
SimpleExample.axaml.cs代碼
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Threading;
using Shares.Avalonia;
using System;
namespace AvaloniaUI;
public partial class SimpleExample : UserControl
{
private readonly SolidColorBrush animatedBrush;
private AnimationPlayer animation;
public SimpleExample()
{
InitializeComponent();
animatedBrush = (SolidColorBrush)Resources["AnimatedBrush"]!;
animation = new AnimationPlayer { Duration = 2.0, Loop = true };
animation.At(0)
.Ease("ElasticEaseOut")
.PlayLocal(progress =>
{
// 顏色平滑過渡
animatedBrush.Color = InterpolateColor(Colors.Red, Colors.Blue, progress);
});
animation.Start();
}
private static Color InterpolateColor(Color from, Color to, double t)
{
byte r = (byte)(from.R + (to.R - from.R) * t);
byte g = (byte)(from.G + (to.G - from.G) * t);
byte b = (byte)(from.B + (to.B - from.B) * t);
return Color.FromRgb(r, g, b);
}
}
運(yùn)行效果

浙公網(wǎng)安備 33010602011771號