From eeff2d7719854737459f4da8c582042d6ba32144 Mon Sep 17 00:00:00 2001 From: Soar360 Date: Thu, 5 Dec 2024 20:11:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20PinCode=20=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 如果粘贴内容有效,则执行 `CompleteCommand` 并触发 `CompleteEvent` 事件。 --- src/Ursa/Controls/PinCode/PinCode.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Ursa/Controls/PinCode/PinCode.cs b/src/Ursa/Controls/PinCode/PinCode.cs index a04fb6d2..a1957be5 100644 --- a/src/Ursa/Controls/PinCode/PinCode.cs +++ b/src/Ursa/Controls/PinCode/PinCode.cs @@ -11,12 +11,12 @@ namespace Ursa.Controls; [TemplatePart(PART_ItemsControl, typeof(ItemsControl))] -public class PinCode: TemplatedControl +public class PinCode : TemplatedControl { public const string PART_ItemsControl = "PART_ItemsControl"; private ItemsControl? _itemsControl; private int _currentIndex; - + public static readonly StyledProperty CompleteCommandProperty = AvaloniaProperty.Register( nameof(CompleteCommand)); @@ -57,18 +57,18 @@ public PinCodeMode Mode public static readonly DirectProperty> DigitsProperty = AvaloniaProperty.RegisterDirect>( nameof(Digits), o => o.Digits); - + private IList _digits = []; public IList Digits { get => _digits; private set => SetAndRaise(DigitsProperty, ref _digits, value); } - + public static readonly RoutedEvent CompleteEvent = RoutedEvent.Register( nameof(Complete), RoutingStrategies.Bubble); - + public event EventHandler Complete { add => AddHandler(CompleteEvent, value); @@ -79,7 +79,7 @@ static PinCode() { CountProperty.Changed.AddClassHandler((code, args) => code.OnCountOfDigitChanged(args)); FocusableProperty.OverrideDefaultValue(true); - KeyDownEvent.AddClassHandler((o,e)=>o.OnPreviewKeyDown(e), RoutingStrategies.Tunnel); + KeyDownEvent.AddClassHandler((o, e) => o.OnPreviewKeyDown(e), RoutingStrategies.Tunnel); } public PinCode() @@ -119,7 +119,7 @@ private void OnControlPressed(object? sender, PointerPressedEventArgs e) _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); } - + } e.Handled = true; } @@ -181,6 +181,11 @@ protected async void OnPreviewKeyDown(KeyEventArgs e) presenter.Text = newText[i].ToString(); } } + if (newText.Length == Count) + { + CompleteCommand?.Execute(Digits); + RaiseEvent(new PinCodeCompleteEventArgs(Digits, CompleteEvent)); + } } return; } @@ -210,7 +215,7 @@ protected async void OnPreviewKeyDown(KeyEventArgs e) _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); } - else if(e.Key is Key.Right or Key.FnRightArrow) + else if (e.Key is Key.Right or Key.FnRightArrow) { _currentIndex++; _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1);