From aa5f0e027631760479f721adb0a736ac07c12716 Mon Sep 17 00:00:00 2001 From: Nlick47 <2247717951@qq.com> Date: Fri, 11 Apr 2025 01:31:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8A=A0=E5=85=A5=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DownKyi/CustomAction/NumericInputBehavior.cs | 49 +++++++++++++++++++ DownKyi/CustomControl/CustomPager.axaml | 31 ++++++++++++ DownKyi/CustomControl/CustomPagerViewModel.cs | 17 +++++++ DownKyi/Languanges/Default.axaml | 6 ++- 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 DownKyi/CustomAction/NumericInputBehavior.cs diff --git a/DownKyi/CustomAction/NumericInputBehavior.cs b/DownKyi/CustomAction/NumericInputBehavior.cs new file mode 100644 index 0000000..4a7a769 --- /dev/null +++ b/DownKyi/CustomAction/NumericInputBehavior.cs @@ -0,0 +1,49 @@ +using System.Linq; +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Interactivity; +using Avalonia.Xaml.Interactivity; + +namespace DownKyi.CustomAction; + +public class NumericInputBehavior : Behavior +{ + protected override void OnAttached() + { + if (AssociatedObject != null) + { + AssociatedObject.AddHandler(InputElement.TextInputEvent, OnTextInput, RoutingStrategies.Tunnel); + AssociatedObject.TextChanging += OnTextChanging; + } + base.OnAttached(); + } + + protected override void OnDetaching() + { + if (AssociatedObject != null) + { + AssociatedObject.RemoveHandler(InputElement.TextInputEvent, OnTextInput); + AssociatedObject.TextChanging -= OnTextChanging; + } + base.OnDetaching(); + } + + private void OnTextInput(object? sender, TextInputEventArgs e) + { + if (string.IsNullOrEmpty(e.Text) || e.Text.All(char.IsControl)) + return; + + if (!char.IsDigit(e.Text[0])) + e.Handled = true; + } + + private void OnTextChanging(object? sender, TextChangingEventArgs e) + { + if (e.Source is TextBox t && + !string.IsNullOrEmpty(t.Text) && !t.Text.All(char.IsDigit)) + { + t.Text = new string(t.Text.Where(char.IsDigit).ToArray()); + t.CaretIndex = t.Text?.Length ?? 0; + } + } +} \ No newline at end of file diff --git a/DownKyi/CustomControl/CustomPager.axaml b/DownKyi/CustomControl/CustomPager.axaml index 8e3def9..6133ccd 100644 --- a/DownKyi/CustomControl/CustomPager.axaml +++ b/DownKyi/CustomControl/CustomPager.axaml @@ -3,6 +3,7 @@ x:Class="DownKyi.CustomControl.CustomPager" xmlns:vmc="clr-namespace:DownKyi.CustomControl" x:DataType="vmc:CustomPagerViewModel" + xmlns:action="clr-namespace:DownKyi.CustomAction" x:CompileBindings="False"> @@ -234,5 +235,35 @@ VerticalAlignment="Center" Source="{StaticResource ToRightDrawingImage}" /> + + + + + + + + + + + + / + + + + + \ No newline at end of file diff --git a/DownKyi/CustomControl/CustomPagerViewModel.cs b/DownKyi/CustomControl/CustomPagerViewModel.cs index 9bdd83f..e9f2db1 100644 --- a/DownKyi/CustomControl/CustomPagerViewModel.cs +++ b/DownKyi/CustomControl/CustomPagerViewModel.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using Avalonia.Controls; using Prism.Commands; namespace DownKyi.CustomControl; @@ -77,6 +78,9 @@ public class CustomPagerViewModel : INotifyPropertyChanged } } } + + + private int _current; @@ -385,7 +389,20 @@ public class CustomPagerViewModel : INotifyPropertyChanged SetView(); } + + private DelegateCommand? _jumpCommand; + public DelegateCommand JumpCommand => _jumpCommand ??= new DelegateCommand(JumpExecuted); + + private void JumpExecuted(object obj) + { + if (obj is string s && int.TryParse(s,out var i)) + { + Current = (i >= _count) ? _count : i; + SetView(); + } + } + /// /// 控制显示,暴力实现,以后重构 /// diff --git a/DownKyi/Languanges/Default.axaml b/DownKyi/Languanges/Default.axaml index 348c95a..3964e6b 100644 --- a/DownKyi/Languanges/Default.axaml +++ b/DownKyi/Languanges/Default.axaml @@ -352,5 +352,9 @@ 全部应用 - + + + + + 跳至 \ No newline at end of file