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