106 lines
3.2 KiB
C#
106 lines
3.2 KiB
C#
using Avalonia.Animation;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Styling;
|
|
using Avalonia.Xaml.Interactivity;
|
|
using Avalonia.Threading;
|
|
|
|
|
|
namespace Cowain.Base.Helpers;
|
|
|
|
public class GridPositionAnimator : Behavior<Control>
|
|
{
|
|
|
|
public static readonly StyledProperty<int> RowProperty =
|
|
AvaloniaProperty.Register<GridPositionAnimator, int>("Row");
|
|
|
|
public static readonly StyledProperty<int> ColumnProperty =
|
|
AvaloniaProperty.Register<GridPositionAnimator, int>("Column");
|
|
|
|
public int Row
|
|
{
|
|
get => GetValue(RowProperty);
|
|
set => SetValue(RowProperty, value);
|
|
}
|
|
|
|
public int Column
|
|
{
|
|
get => GetValue(ColumnProperty);
|
|
set => SetValue(ColumnProperty, value);
|
|
}
|
|
|
|
protected override void OnAttached()
|
|
{
|
|
base.OnAttached();
|
|
if (AssociatedObject != null)
|
|
{
|
|
Dispatcher.UIThread.Post(() =>
|
|
{
|
|
// 初始设置位置
|
|
Grid.SetRow(AssociatedObject, Row);
|
|
Grid.SetColumn(AssociatedObject, Column);
|
|
|
|
// 新的事件订阅方式
|
|
RowProperty.Changed.AddClassHandler<GridPositionAnimator>((b, e) => b.OnRowChanged(e));
|
|
ColumnProperty.Changed.AddClassHandler<GridPositionAnimator>((b, e) => b.OnColumnChanged(e));
|
|
});
|
|
}
|
|
}
|
|
|
|
private void OnRowChanged(AvaloniaPropertyChangedEventArgs e)
|
|
{
|
|
if (AssociatedObject != null && e.NewValue is int newRow)
|
|
{
|
|
// 创建行变化动画
|
|
var animation = new Animation
|
|
{
|
|
Duration = TimeSpan.FromSeconds(1),
|
|
FillMode = FillMode.Both,
|
|
Children =
|
|
{
|
|
new KeyFrame
|
|
{
|
|
Cue = new Cue(0d),
|
|
Setters = { new Setter { Property = Grid.RowProperty, Value = e.OldValue } }
|
|
},
|
|
new KeyFrame
|
|
{
|
|
Cue = new Cue(1d),
|
|
Setters = { new Setter { Property = Grid.RowProperty, Value = newRow } }
|
|
}
|
|
}
|
|
};
|
|
|
|
animation.RunAsync(AssociatedObject);
|
|
}
|
|
}
|
|
|
|
private void OnColumnChanged(AvaloniaPropertyChangedEventArgs e)
|
|
{
|
|
if (AssociatedObject != null && e.NewValue is int newColumn)
|
|
{
|
|
// 创建列变化动画
|
|
var animation = new Animation
|
|
{
|
|
Duration = TimeSpan.FromSeconds(1),
|
|
FillMode = FillMode.Both,
|
|
Children =
|
|
{
|
|
new KeyFrame
|
|
{
|
|
Cue = new Cue(0d),
|
|
Setters = { new Setter { Property = Grid.ColumnProperty, Value = e.OldValue } }
|
|
},
|
|
new KeyFrame
|
|
{
|
|
Cue = new Cue(1d),
|
|
Setters = { new Setter { Property = Grid.ColumnProperty, Value = newColumn } }
|
|
}
|
|
}
|
|
};
|
|
|
|
animation.RunAsync(AssociatedObject);
|
|
}
|
|
}
|
|
}
|