From b9df455c1b29903c88a49bccef9b2cafd64a0f91 Mon Sep 17 00:00:00 2001 From: Nlick47 <2247717951@qq.com> Date: Mon, 17 Mar 2025 16:03:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=BC=E8=88=AA=E7=A6=BB=E5=BC=80?= =?UTF-8?q?=E6=88=96=E9=A1=B5=E9=9D=A2=E5=88=B7=E6=96=B0=E9=87=8D=E7=BD=AE?= =?UTF-8?q?=E5=88=86=E5=89=B2=E5=99=A8=E7=9A=84=E5=BD=B1=E5=93=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomAction/GridSplitterExtensions.cs | 45 +++++++++++++ .../CustomAction/ResetGridSplitterBehavior.cs | 64 +++++++++++++++++++ .../ViewModels/ViewVideoDetailViewModel.cs | 14 ++-- DownKyi/Views/ViewVideoDetail.axaml | 21 ++++-- 4 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 DownKyi/CustomAction/GridSplitterExtensions.cs create mode 100644 DownKyi/CustomAction/ResetGridSplitterBehavior.cs diff --git a/DownKyi/CustomAction/GridSplitterExtensions.cs b/DownKyi/CustomAction/GridSplitterExtensions.cs new file mode 100644 index 0000000..4e8ad68 --- /dev/null +++ b/DownKyi/CustomAction/GridSplitterExtensions.cs @@ -0,0 +1,45 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Data; +using Avalonia.Xaml.Interactivity; + +namespace DownKyi.CustomAction; + +public class GridSplitterExtensions +{ + public static readonly AttachedProperty ResetGridBehaviorProperty = + AvaloniaProperty.RegisterAttached( + "ResetGridBehavior", coerce: OnResetGridBehaviorChanged); + + public static ResetGridSplitterBehavior GetResetGridBehavior(GridSplitter obj) + { + return obj.GetValue(ResetGridBehaviorProperty); + } + + public static void SetResetGridBehavior(GridSplitter obj, ResetGridSplitterBehavior value) + { + obj.SetValue(ResetGridBehaviorProperty, value); + } + + private static ResetGridSplitterBehavior OnResetGridBehaviorChanged(AvaloniaObject obj, ResetGridSplitterBehavior behavior) + { + if (obj is GridSplitter gridSplitter) + { + var oldBehavior = GetResetGridBehavior(gridSplitter); + + if (oldBehavior != null) + { + var behaviors = Interaction.GetBehaviors(gridSplitter); + behaviors.Remove(oldBehavior); + } + + if (behavior != null) + { + var behaviors = Interaction.GetBehaviors(gridSplitter); + behaviors.Add(behavior); + } + } + + return behavior; + } +} \ No newline at end of file diff --git a/DownKyi/CustomAction/ResetGridSplitterBehavior.cs b/DownKyi/CustomAction/ResetGridSplitterBehavior.cs new file mode 100644 index 0000000..b7402eb --- /dev/null +++ b/DownKyi/CustomAction/ResetGridSplitterBehavior.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Xaml.Interactivity; + +namespace DownKyi.CustomAction; +public class ResetGridSplitterBehavior : Behavior +{ + private Dictionary _originalColumnWidths = new (); + private Dictionary _originalRowHeights = new (); + private Grid _parentGrid; + + protected override void OnAttached() + { + base.OnAttached(); + var gridSplitter = AssociatedObject; + _parentGrid = gridSplitter.Parent as Grid; + + if (_parentGrid != null) + { + for (int i = 0; i < _parentGrid.ColumnDefinitions.Count; i++) + { + _originalColumnWidths[i] = _parentGrid.ColumnDefinitions[i].Width; + } + + for (int i = 0; i < _parentGrid.RowDefinitions.Count; i++) + { + _originalRowHeights[i] = _parentGrid.RowDefinitions[i].Height; + } + } + + } + + private void OnRefreshRequested(object sender, EventArgs e) + { + ResetGrid(); + } + + public void ResetGrid() + { + if (_parentGrid != null) + { + foreach (var kvp in _originalColumnWidths) + { + _parentGrid.ColumnDefinitions[kvp.Key].Width = kvp.Value; + } + + foreach (var kvp in _originalRowHeights) + { + _parentGrid.RowDefinitions[kvp.Key].Height = kvp.Value; + } + } + } + + protected override void OnDetachedFromVisualTree() + { + base.OnDetachedFromVisualTree(); + ResetGrid(); + } + + +} diff --git a/DownKyi/ViewModels/ViewVideoDetailViewModel.cs b/DownKyi/ViewModels/ViewVideoDetailViewModel.cs index 6244a19..cf78990 100644 --- a/DownKyi/ViewModels/ViewVideoDetailViewModel.cs +++ b/DownKyi/ViewModels/ViewVideoDetailViewModel.cs @@ -10,6 +10,7 @@ using DownKyi.Core.BiliApi.BiliUtils; using DownKyi.Core.BiliApi.VideoStream; using DownKyi.Core.Logging; using DownKyi.Core.Settings; +using DownKyi.CustomAction; using DownKyi.Events; using DownKyi.Images; using DownKyi.Services; @@ -121,6 +122,9 @@ public class ViewVideoDetailViewModel : ViewModelBase set => SetProperty(ref _noDataVisibility, value); } + + public ResetGridSplitterBehavior ResetGridBehavior { get; set; } = new(); + #endregion public ViewVideoDetailViewModel(IEventAggregator eventAggregator, IDialogService dialogService) : base(eventAggregator, dialogService) @@ -186,6 +190,7 @@ public class ViewVideoDetailViewModel : ViewModelBase private DelegateCommand? _inputSearchCommand; + public DelegateCommand InputSearchCommand => _inputSearchCommand ??= new DelegateCommand(ExecuteInputSearchCommand); @@ -351,7 +356,7 @@ public class ViewVideoDetailViewModel : ViewModelBase } var section = VideoSections.FirstOrDefault(item => item.IsSelected); - + if (section == null) { return; @@ -391,7 +396,7 @@ public class ViewVideoDetailViewModel : ViewModelBase // 解析视频流事件 private DelegateCommand? _parseCommand; - + public DelegateCommand ParseCommand => _parseCommand ??= new DelegateCommand(ExecuteParseCommand, CanExecuteParseCommand); /// @@ -583,14 +588,15 @@ public class ViewVideoDetailViewModel : ViewModelBase private void InitView() { LogManager.Debug(Tag, "初始化页面元素"); - + ResetGridBehavior.ResetGrid(); LoadingVisibility = true; ContentVisibility = false; NoDataVisibility = false; - VideoSections.Clear(); CaCheVideoSections.Clear(); } + + /// /// 更新页面的统一方法 diff --git a/DownKyi/Views/ViewVideoDetail.axaml b/DownKyi/Views/ViewVideoDetail.axaml index 289a1f0..7205510 100644 --- a/DownKyi/Views/ViewVideoDetail.axaml +++ b/DownKyi/Views/ViewVideoDetail.axaml @@ -6,6 +6,7 @@ xmlns:i="using:Avalonia.Xaml.Interactivity" xmlns:ia="clr-namespace:Avalonia.Xaml.Interactions.Core;assembly=Avalonia.Xaml.Interactions" xmlns:custom="clr-namespace:DownKyi.CustomControl" + xmlns:behavior="clr-namespace:DownKyi.CustomAction" xmlns:iac="clr-namespace:Avalonia.Xaml.Interactions.Custom;assembly=Avalonia.Xaml.Interactions.Custom" x:DataType="vm:ViewVideoDetailViewModel"> @@ -252,12 +253,13 @@ - - + + - + + + - +