feat: add a sample for date.

This commit is contained in:
rabbitism
2024-02-17 15:16:49 +08:00
parent 35d0d5a7b1
commit e7b99842bc
3 changed files with 33 additions and 3 deletions

View File

@@ -12,5 +12,6 @@
<Button Command="{Binding IncreaseCommand}" >Change</Button> <Button Command="{Binding IncreaseCommand}" >Change</Button>
<u:Int32Displayer Value="{Binding Value}"></u:Int32Displayer> <u:Int32Displayer Value="{Binding Value}"></u:Int32Displayer>
<u:DoubleDisplayer Value="{Binding DoubleValue}" StringFormat="N2"></u:DoubleDisplayer> <u:DoubleDisplayer Value="{Binding DoubleValue}" StringFormat="N2"></u:DoubleDisplayer>
<u:DateDisplay Value="{Binding DateValue}" StringFormat="yyyy-MM-dd"></u:DateDisplay>
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -9,12 +9,14 @@ public partial class NumberDisplayerDemoViewModel: ObservableObject
{ {
[ObservableProperty] private int _value; [ObservableProperty] private int _value;
[ObservableProperty] private double _doubleValue; [ObservableProperty] private double _doubleValue;
[ObservableProperty] private DateTime _dateValue;
public ICommand IncreaseCommand { get; } public ICommand IncreaseCommand { get; }
public NumberDisplayerDemoViewModel() public NumberDisplayerDemoViewModel()
{ {
IncreaseCommand = new RelayCommand(OnChange); IncreaseCommand = new RelayCommand(OnChange);
Value = 0; Value = 0;
DoubleValue = 0d; DoubleValue = 0d;
DateValue = DateTime.Now;
} }
private void OnChange() private void OnChange()
@@ -22,5 +24,6 @@ public partial class NumberDisplayerDemoViewModel: ObservableObject
Random r = new Random(); Random r = new Random();
Value = r.Next(int.MaxValue); Value = r.Next(int.MaxValue);
DoubleValue = r.NextDouble() * 100000; DoubleValue = r.NextDouble() * 100000;
DateValue = DateTime.Today.AddDays(r.Next(1000));
} }
} }

View File

@@ -91,6 +91,8 @@ public abstract class NumberDisplayer<T>: NumberDisplayerBase
Setters = { new Setter{Property = InternalValueProperty } } Setters = { new Setter{Property = InternalValueProperty } }
}); });
Animation.SetAnimator(_animation.Children[0].Setters[0], GetAnimator()); Animation.SetAnimator(_animation.Children[0].Setters[0], GetAnimator());
Animation.SetAnimator(_animation.Children[1].Setters[0], GetAnimator());
InternalValue = Value;
} }
private void OnDurationChanged(AvaloniaPropertyChangedEventArgs<TimeSpan> args) private void OnDurationChanged(AvaloniaPropertyChangedEventArgs<TimeSpan> args)
@@ -101,11 +103,12 @@ public abstract class NumberDisplayer<T>: NumberDisplayerBase
protected virtual void OnValueChanged(T? oldValue, T? newValue) protected virtual void OnValueChanged(T? oldValue, T? newValue)
{ {
if (_animation is null) return;
_cts.Cancel(); _cts.Cancel();
_cts = new CancellationTokenSource(); _cts = new CancellationTokenSource();
(_animation?.Children[0].Setters[0] as Setter)!.Value = oldValue; (_animation.Children[0].Setters[0] as Setter)!.Value = oldValue;
(_animation?.Children[1].Setters[0] as Setter)!.Value = newValue; (_animation.Children[1].Setters[0] as Setter)!.Value = newValue;
_animation?.RunAsync(this, _cts.Token); _animation.RunAsync(this, _cts.Token);
} }
protected abstract InterpolatingAnimator<T> GetAnimator(); protected abstract InterpolatingAnimator<T> GetAnimator();
@@ -159,3 +162,26 @@ public class DoubleDisplayer : NumberDisplayer<double>
return value.ToString(StringFormat); return value.ToString(StringFormat);
} }
} }
public class DateDisplay : NumberDisplayer<DateTime>
{
protected override Type StyleKeyOverride { get; } = typeof(NumberDisplayerBase);
protected override InterpolatingAnimator<DateTime> GetAnimator()
{
return new DateAnimator();
}
private class DateAnimator : InterpolatingAnimator<DateTime>
{
public override DateTime Interpolate(double progress, DateTime oldValue, DateTime newValue)
{
return oldValue + TimeSpan.FromTicks((long)((newValue - oldValue).Ticks * progress));
}
}
protected override string GetString(DateTime value)
{
return value.ToString(StringFormat);
}
}