feat: add a sample for date.
This commit is contained in:
@@ -12,5 +12,6 @@
|
||||
<Button Command="{Binding IncreaseCommand}" >Change</Button>
|
||||
<u:Int32Displayer Value="{Binding Value}"></u:Int32Displayer>
|
||||
<u:DoubleDisplayer Value="{Binding DoubleValue}" StringFormat="N2"></u:DoubleDisplayer>
|
||||
<u:DateDisplay Value="{Binding DateValue}" StringFormat="yyyy-MM-dd"></u:DateDisplay>
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
|
||||
@@ -9,12 +9,14 @@ public partial class NumberDisplayerDemoViewModel: ObservableObject
|
||||
{
|
||||
[ObservableProperty] private int _value;
|
||||
[ObservableProperty] private double _doubleValue;
|
||||
[ObservableProperty] private DateTime _dateValue;
|
||||
public ICommand IncreaseCommand { get; }
|
||||
public NumberDisplayerDemoViewModel()
|
||||
{
|
||||
IncreaseCommand = new RelayCommand(OnChange);
|
||||
Value = 0;
|
||||
DoubleValue = 0d;
|
||||
DateValue = DateTime.Now;
|
||||
}
|
||||
|
||||
private void OnChange()
|
||||
@@ -22,5 +24,6 @@ public partial class NumberDisplayerDemoViewModel: ObservableObject
|
||||
Random r = new Random();
|
||||
Value = r.Next(int.MaxValue);
|
||||
DoubleValue = r.NextDouble() * 100000;
|
||||
DateValue = DateTime.Today.AddDays(r.Next(1000));
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,8 @@ public abstract class NumberDisplayer<T>: NumberDisplayerBase
|
||||
Setters = { new Setter{Property = InternalValueProperty } }
|
||||
});
|
||||
Animation.SetAnimator(_animation.Children[0].Setters[0], GetAnimator());
|
||||
Animation.SetAnimator(_animation.Children[1].Setters[0], GetAnimator());
|
||||
InternalValue = Value;
|
||||
}
|
||||
|
||||
private void OnDurationChanged(AvaloniaPropertyChangedEventArgs<TimeSpan> args)
|
||||
@@ -101,11 +103,12 @@ public abstract class NumberDisplayer<T>: NumberDisplayerBase
|
||||
|
||||
protected virtual void OnValueChanged(T? oldValue, T? newValue)
|
||||
{
|
||||
if (_animation is null) return;
|
||||
_cts.Cancel();
|
||||
_cts = new CancellationTokenSource();
|
||||
(_animation?.Children[0].Setters[0] as Setter)!.Value = oldValue;
|
||||
(_animation?.Children[1].Setters[0] as Setter)!.Value = newValue;
|
||||
_animation?.RunAsync(this, _cts.Token);
|
||||
(_animation.Children[0].Setters[0] as Setter)!.Value = oldValue;
|
||||
(_animation.Children[1].Setters[0] as Setter)!.Value = newValue;
|
||||
_animation.RunAsync(this, _cts.Token);
|
||||
}
|
||||
|
||||
protected abstract InterpolatingAnimator<T> GetAnimator();
|
||||
@@ -159,3 +162,26 @@ public class DoubleDisplayer : NumberDisplayer<double>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user