Skip to content

Commit b33d99a

Browse files
committed
add: 分包数据自动滚动逻辑
1 parent 8ca6f02 commit b33d99a

File tree

4 files changed

+67
-31
lines changed

4 files changed

+67
-31
lines changed

llcomNext/LLCOM/Services/Utils.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Avalonia;
77
using Avalonia.Controls;
88
using Avalonia.Controls.ApplicationLifetimes;
9+
using LLCOM.Models;
910
using RestSharp;
1011

1112
namespace LLCOM.Services;
@@ -36,7 +37,15 @@ private static string InitializeAppPath()
3637
return appPath;
3738
}
3839

40+
/// <summary>
41+
/// 全局设置
42+
/// </summary>
3943
public static Setting Setting = null!;
44+
45+
/// <summary>
46+
/// 添加数据包到分包数据界面
47+
/// </summary>
48+
public static Action<PackData> AddPacketDataAction = null!;
4049

4150
/// <summary>
4251
/// 启动软件时的初始化操作

llcomNext/LLCOM/ViewModels/DataViews/PacketDataViewModel.cs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.ObjectModel;
33
using System.Threading.Tasks;
44
using CommunityToolkit.Mvvm.ComponentModel;
5+
using CommunityToolkit.Mvvm.Input;
56
using LLCOM.Models;
67

78
namespace LLCOM.ViewModels;
@@ -17,41 +18,32 @@ public PacketDataViewModel(Func<Type, ViewModelBase> getService)
1718
{
1819
_getService = getService;
1920

20-
Task.Run(async () =>
21+
//添加数据包到分包数据界面的操作
22+
Services.Utils.AddPacketDataAction = data =>
2123
{
22-
for(int i=0; i<100; i++)
23-
{
24-
await Task.Delay(100);
25-
for(int j=0; j<100; j++)
26-
{
27-
PacketData.Add(new ("0123"u8.ToArray(), MessageWay.Send, "串口1"));
28-
PacketData.Add(new ("0123"u8.ToArray(), MessageWay.Receive, "串口1"));
29-
}
30-
}
31-
});
24+
lock (PacketData)
25+
PacketData.Add(data);
26+
if(AutoScroll)
27+
ScrollToBottomAction?.Invoke();
28+
};
3229
}
3330

3431
[ObservableProperty]
35-
private ObservableCollection<PackData> _packetData = [
36-
new ([], MessageWay.Unknown, "MQTT1",null,null,true,"已连接"),
37-
new ("0123"u8.ToArray(), MessageWay.Send, "串口1"),
38-
new ("0123"u8.ToArray(), MessageWay.Receive, "串口1"),
39-
new ("0123"u8.ToArray(), MessageWay.Receive, "串口1"),
40-
new ("0123"u8.ToArray(), MessageWay.Send, "串口1"),
41-
new ("0123"u8.ToArray(), MessageWay.Receive, "串口1"),
42-
new ("0123"u8.ToArray(), MessageWay.Send, "串口1"),
43-
new ("0123"u8.ToArray(), MessageWay.Receive, "串口1"),
44-
new ("0123"u8.ToArray(), MessageWay.Send, "串口1"),
45-
new ("0123"u8.ToArray(), MessageWay.Receive, "串口1"),
46-
new ("0123"u8.ToArray(), MessageWay.Send, "串口1"),
47-
new ("0123"u8.ToArray(), MessageWay.Receive, "串口1"),
48-
new ("0123"u8.ToArray(), MessageWay.Send, "串口1"),
49-
new ("0123"u8.ToArray(), MessageWay.Receive, "串口1"),
50-
new ("0123"u8.ToArray(), MessageWay.Send, "串口1"),
51-
new ("0123"u8.ToArray(), MessageWay.Receive, "串口1"),
52-
];
32+
private ObservableCollection<PackData> _packetData = [];
5333

5434
//自动滚到底部
5535
[ObservableProperty]
5636
private bool _autoScroll = true;
37+
38+
public Action? ScrollToBottomAction { get; set; }
39+
40+
[RelayCommand]
41+
private async Task ClearPacketData()
42+
{
43+
await Task.Run(() =>
44+
{
45+
lock (PacketData)
46+
PacketData.Clear();
47+
});
48+
}
5749
}

llcomNext/LLCOM/Views/DataViews/PacketDataView.axaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
d:DesignHeight="757"
1111
d:DesignWidth="762"
1212
x:DataType="vm:PacketDataViewModel"
13+
Loaded="Control_OnLoaded"
1314
mc:Ignorable="d">
1415
<Design.DataContext>
1516
<vm:PacketDataViewModel />
@@ -44,6 +45,7 @@
4445
</UserControl.Styles>
4546
<Grid RowDefinitions="auto *">
4647
<ListBox
48+
Name="MainListBox"
4749
Grid.Row="1"
4850
Margin="0,5"
4951
u:ScrollTo.ButtonTheme="{DynamicResource PrimaryScrollToButton}"
@@ -98,7 +100,7 @@
98100
VerticalAlignment="Center"
99101
Content="自动滚动"
100102
IsChecked="{Binding AutoScroll}" />
101-
<Button Content="清空数据" />
103+
<Button Command="{Binding ClearPacketDataCommand}" Content="清空数据" />
102104
</StackPanel>
103105
</Border>
104106
</Grid>
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
1-
using Avalonia;
1+
using System;
2+
using System.Linq;
3+
using Avalonia;
24
using Avalonia.Controls;
5+
using Avalonia.Interactivity;
36
using Avalonia.Markup.Xaml;
7+
using Avalonia.Threading;
8+
using LLCOM.ViewModels;
49

510
namespace LLCOM.Views;
611

712
public partial class PacketDataView : UserControl
813
{
14+
915
public PacketDataView()
1016
{
1117
InitializeComponent();
1218
}
19+
20+
private bool _needScrollToBottom = false;
21+
private void Control_OnLoaded(object? sender, RoutedEventArgs e)
22+
{
23+
if (DataContext is PacketDataViewModel vm)
24+
vm.ScrollToBottomAction = () => _needScrollToBottom = true;
25+
26+
27+
//开一个定时器,定时检查是否需要滚动到底部
28+
DispatcherTimer timer = new()
29+
{
30+
Interval = TimeSpan.FromMilliseconds(100)
31+
};
32+
timer.Tick += (_, args) =>
33+
{
34+
if (_needScrollToBottom)
35+
{
36+
Dispatcher.UIThread.Post(() =>
37+
{
38+
if (MainListBox.ItemCount > 0)
39+
MainListBox.ScrollIntoView(MainListBox.ItemCount - 1);
40+
}, DispatcherPriority.Background);
41+
_needScrollToBottom = false;
42+
}
43+
};
44+
timer.Start();
45+
}
1346
}

0 commit comments

Comments
 (0)