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="可复用跟随效果">
            <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);
    }
}

运行效果

image

 

posted on 2025-10-21 09:02  dalgleish  阅读(3)  评论(0)    收藏  举报