Skip to content

Commit 10b2732

Browse files
committed
add: listbox展示数据
1 parent 188f58c commit 10b2732

File tree

9 files changed

+183
-109
lines changed

9 files changed

+183
-109
lines changed

llcomNext/LLCOM/Controls/PacketDataControl.axaml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:controls="using:LLCOM.Controls">
55
<Design.PreviewWith>
6-
<Grid Width="500">
6+
<StackPanel Width="500">
77
<controls:PacketDataControl
88
Extra="2025/03/11 - 11:22:33.123"
99
Header="本机 &lt;&lt; 串口1"
1010
Hex="11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF"
1111
MainColor="{DynamicResource SemiGreen8}"
1212
Text="接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123" />
13-
</Grid>
13+
<controls:PacketDataControl
14+
Extra="2025/03/11 - 11:22:33.123"
15+
Header="本机 &lt;&lt; 串口1"
16+
Hex="44 55 66 77 88 99 00 AA BB CC DD EE FF"
17+
MainColor="{DynamicResource SemiGreen8}"
18+
Text="接收到的xxx数据123123接" />
19+
</StackPanel>
1420
</Design.PreviewWith>
1521

1622
<Style Selector="controls|PacketDataControl">
@@ -30,12 +36,12 @@
3036
Foreground="{DynamicResource SemiColorHighlight}"
3137
Text="{TemplateBinding Header}" />
3238
</Border>
33-
<TextBlock
39+
<SelectableTextBlock
3440
Margin="0,2,10,0"
3541
HorizontalAlignment="Right"
3642
FontSize="12"
3743
FontWeight="Bold"
38-
Text="{DynamicResource Extra}" />
44+
Text="{TemplateBinding Extra}" />
3945
</Grid>
4046
<Border
4147
BorderBrush="{TemplateBinding MainColor}"
@@ -44,12 +50,14 @@
4450
<StackPanel>
4551
<SelectableTextBlock
4652
Margin="5,5,5,0"
53+
Background="Transparent"
4754
FontSize="16"
4855
Foreground="{TemplateBinding MainColor}"
4956
Text="{TemplateBinding Text}"
5057
TextWrapping="Wrap" />
5158
<SelectableTextBlock
5259
Margin="5,0,5,3"
60+
Background="Transparent"
5361
FontSize="12"
5462
Foreground="{DynamicResource SemiColorText2}"
5563
Text="{TemplateBinding Hex}"

llcomNext/LLCOM/Models/PacketData.cs

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using Avalonia.Media;
56

67
namespace LLCOM.Models;
78

89
public class PacketData
910
{
11+
public PacketData(byte[] data, MessageWay way, string channel, string? extra = null,
12+
Encoding? encoding = null, bool readable = true, string? s = null, IBrush? brush = null)
13+
{
14+
Data = data;
15+
Way = way;
16+
Channel = channel;
17+
Extra = extra ?? Time.ToString("yyyy/MM/dd HH:mm:ss.fff");
18+
encoding ??= Encoding.UTF8;
19+
String = s ?? GenerateString(Data, encoding, readable);
20+
HexString = GenerateHexString(Data);
21+
}
22+
1023
/// <summary>
1124
/// 此包收到的时间
1225
/// </summary>
@@ -15,12 +28,14 @@ public class PacketData
1528
/// <summary>
1629
/// 包内的原始数据
1730
/// </summary>
18-
public byte[] Data { get; set; } = [];
31+
public byte[] Data { get; set; }
32+
33+
public string HexString { get; }
1934

2035
/// <summary>
21-
/// 包的额外信息
36+
/// 包的额外信息,一般是日期时间的字符串展示
2237
/// </summary>
23-
public string? Extra { get; set; } = null;
38+
public string Extra { get; set; }
2439

2540
/// <summary>
2641
/// 该包的字符串表示
@@ -36,19 +51,36 @@ public class PacketData
3651
/// 消息通道类型
3752
/// </summary>
3853
public string Channel { get; set; } = string.Empty;
54+
55+
/// <summary>
56+
/// 表示该包的方向和通道的字符串
57+
/// </summary>
58+
public string TagString
59+
{
60+
get
61+
{
62+
return Way switch
63+
{
64+
MessageWay.Unknown => $"{Channel}",
65+
MessageWay.Send => $"本机 >> {Channel}",//TODO 多语言支持
66+
MessageWay.Receive => $"{Channel} >> 本机",//TODO 多语言支持
67+
_ => throw new ArgumentOutOfRangeException()
68+
};
69+
}
70+
}
3971

4072
/// <summary>
4173
/// 根据Data生成一个十六进制字符串
4274
/// </summary>
43-
public void GenerateHexString()
75+
public static string GenerateHexString(byte[] data)
4476
{
4577
var sb = new StringBuilder();
46-
foreach (var b in Data)
78+
foreach (var b in data)
4779
{
4880
sb.Append(b.ToString("X2"));
4981
sb.Append(" ");
5082
}
51-
String = sb.ToString();
83+
return sb.ToString();
5284
}
5385

5486
private static readonly byte[] BDel = "\u2421"u8.ToArray();
@@ -74,26 +106,25 @@ public void GenerateHexString()
74106
/// </summary>
75107
/// <param name="encoding">指定的编码</param>
76108
/// <param name="readable">是否将不可见字符转义为可见字符</param>
77-
/// <returns></returns>
78-
public void GenerateString(Encoding encoding, bool readable = true)
109+
/// <returns>转换后的字符串</returns>
110+
public static string GenerateString(byte[] data,Encoding encoding, bool readable = true)
79111
{
80112
//非utf8编码就不转义了
81113
if (!readable || encoding.CodePage != 65001)
82114
{
83-
String = Byte2String(encoding, Data, true);
84-
return;
115+
return Byte2String(encoding, data, true);
85116
}
86117
var temp = new List<byte>();
87-
for (int i = 0; i < Data.Length; i++)
118+
for (int i = 0; i < data.Length; i++)
88119
{
89-
switch(Data[i])
120+
switch(data[i])
90121
{
91122
case 0x00:
92123
temp.AddRange(Symbols[0x00]);
93124
break;
94125
case 0x0d:
95126
//遇到成对出现
96-
if(i < Data.Length - 1 && Data[i+1] == 0x0a)
127+
if(i < data.Length - 1 && data[i+1] == 0x0a)
97128
{
98129
temp.AddRange(Symbols[0x0d]);
99130
temp.AddRange(Symbols[0x0a]);
@@ -104,26 +135,26 @@ public void GenerateString(Encoding encoding, bool readable = true)
104135
else
105136
{
106137
temp.AddRange(Symbols[0x0d]);
107-
temp.Add(Data[i]);
138+
temp.Add(data[i]);
108139
}
109140
break;
110141
case 0x0a:
111142
case 0x09://tab字符
112-
temp.AddRange(Symbols[Data[i]]);
113-
temp.Add(Data[i]);
143+
temp.AddRange(Symbols[data[i]]);
144+
temp.Add(data[i]);
114145
break;
115146
default:
116147
//普通的字符
117-
if(Data[i] <= 0x1f)
118-
temp.AddRange(Symbols[Data[i]]);
119-
else if (Data[i] == 0x7f)//del
148+
if(data[i] <= 0x1f)
149+
temp.AddRange(Symbols[data[i]]);
150+
else if (data[i] == 0x7f)//del
120151
temp.AddRange(BDel);
121152
else
122-
temp.Add(Data[i]);
153+
temp.Add(data[i]);
123154
break;
124155
}
125156
}
126-
String = Byte2String(encoding, temp.ToArray());
157+
return Byte2String(encoding, temp.ToArray());
127158
}
128159

129160
/// <summary>

llcomNext/LLCOM/Services/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace LLCOM.Services;
1111

1212
public class Utils
1313
{
14-
private static string? _version = null;
14+
private static string? _version = "1.0.0.0";//null;
1515
/// <summary>
1616
/// 软件版本
1717
/// </summary>

llcomNext/LLCOM/ViewModels/DataViews/PacketDataViewModel.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Collections.ObjectModel;
3+
using CommunityToolkit.Mvvm.ComponentModel;
4+
using LLCOM.Models;
25

36
namespace LLCOM.ViewModels;
47

@@ -12,6 +15,27 @@ public PacketDataViewModel() {}
1215
public PacketDataViewModel(Func<Type, ViewModelBase> getService)
1316
{
1417
_getService = getService;
18+
19+
1520
}
1621

22+
[ObservableProperty]
23+
private ObservableCollection<PacketData> _packetData = [
24+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Send, "串口1"),
25+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Receive, "串口1"),
26+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Send, "串口1"),
27+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Receive, "串口1"),
28+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Send, "串口1"),
29+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Receive, "串口1"),
30+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Send, "串口1"),
31+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Receive, "串口1"),
32+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Send, "串口1"),
33+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Receive, "串口1"),
34+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Send, "串口1"),
35+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Receive, "串口1"),
36+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Send, "串口1"),
37+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Receive, "串口1"),
38+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Send, "串口1"),
39+
new PacketData([0x30, 0x31, 0x32, 0x33], MessageWay.Receive, "串口1"),
40+
];
1741
}

llcomNext/LLCOM/ViewModels/MainViewModel.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
using System.Linq;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using Avalonia.Controls.Notifications;
78
using CommunityToolkit.Mvvm.ComponentModel;
89
using CommunityToolkit.Mvvm.Input;
10+
using Notification = Ursa.Controls.Notification;
11+
using WindowNotificationManager = Ursa.Controls.WindowNotificationManager;
912

1013
namespace LLCOM.ViewModels;
1114

@@ -39,13 +42,14 @@ public MainViewModel(Func<Type, ViewModelBase> getService)
3942
_getService = getService;
4043
CurrentPage = _getService(typeof(DataPageViewModel));
4144
CurrentDataPage = _getService(typeof(PacketDataViewModel));
42-
43-
Task.Run(async () =>
44-
{
45-
//检查下是否有新版本吧
46-
if (await Services.Utils.CheckUpdate() != null)
47-
ShowSettingDot = true;
48-
});
45+
}
46+
47+
[RelayCommand]
48+
private async Task PageLoaded()
49+
{
50+
//检查下是否有新版本吧
51+
if (await Services.Utils.CheckUpdate() != null)
52+
ShowSettingDot = true;
4953
}
5054

5155
[RelayCommand]

llcomNext/LLCOM/Views/DataViews/PacketDataView.axaml

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xmlns:controls="clr-namespace:LLCOM.Controls"
66
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:u="https://irihi.tech/ursa"
89
xmlns:vm="using:LLCOM.ViewModels"
910
d:DesignHeight="757"
1011
d:DesignWidth="762"
@@ -13,20 +14,47 @@
1314
<Design.DataContext>
1415
<vm:PacketDataViewModel />
1516
</Design.DataContext>
16-
<ScrollViewer Margin="5,5,10,5">
17-
<StackPanel Spacing="5">
18-
<PacketDataControl
19-
Extra="2025/03/11 - 11:22:33.123"
20-
Header="本机 &lt;&lt; 串口1"
21-
Hex="11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF"
22-
MainColor="{DynamicResource SemiGreen8}"
23-
Text="接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123" />
24-
<PacketDataControl
25-
Extra="2025/03/11 - 11:22:33.123"
26-
Header="串口1 &gt;&gt; 本机"
27-
Hex="11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF"
28-
MainColor="{DynamicResource SemiRed8}"
29-
Text="接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123" />
30-
</StackPanel>
31-
</ScrollViewer>
17+
<!-- <ScrollViewer Margin="5,5,10,5"> -->
18+
<!-- <StackPanel Spacing="5"> -->
19+
<!-- <PacketDataControl -->
20+
<!-- Extra="2025/03/11 - 11:22:33.123" -->
21+
<!-- Header="本机 &lt;&lt; 串口1" -->
22+
<!-- Hex="11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF" -->
23+
<!-- MainColor="{DynamicResource SemiGreen8}" -->
24+
<!-- Text="接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123" /> -->
25+
<!-- <PacketDataControl -->
26+
<!-- Extra="2025/03/11 - 11:22:33.123" -->
27+
<!-- Header="串口1 &gt;&gt; 本机" -->
28+
<!-- Hex="11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF11 22 33 44 55 66 77 88 99 00 AA BB CC DD EE FF" -->
29+
<!-- MainColor="{DynamicResource SemiRed8}" -->
30+
<!-- Text="接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123接收到的xxx数据123123" /> -->
31+
<!-- </StackPanel> -->
32+
<!-- </ScrollViewer> -->
33+
<UserControl.Styles>
34+
<Style Selector="ListBoxItem">
35+
<Setter Property="Padding" Value="0 0 0 5" />
36+
</Style>
37+
<Style Selector="ListBoxItem:pointerover">
38+
<Setter Property="Background" Value="Transparent" />
39+
</Style>
40+
<Style Selector="ListBoxItem:selected">
41+
<Setter Property="Background" Value="Transparent" />
42+
</Style>
43+
</UserControl.Styles>
44+
<ListBox
45+
Margin="5,5,10,5"
46+
u:ScrollTo.ButtonTheme="{DynamicResource PrimaryScrollToButton}"
47+
u:ScrollTo.Direction="Bottom"
48+
ItemsSource="{Binding PacketData}">
49+
<ListBox.ItemTemplate>
50+
<DataTemplate>
51+
<controls:PacketDataControl
52+
Extra="{Binding Extra}"
53+
Header="{Binding TagString}"
54+
Hex="{Binding HexString}"
55+
MainColor="{DynamicResource SemiGreen8}"
56+
Text="{Binding String}" />
57+
</DataTemplate>
58+
</ListBox.ItemTemplate>
59+
</ListBox>
3260
</UserControl>

llcomNext/LLCOM/Views/MainView.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
d:DesignHeight="800"
1010
d:DesignWidth="1200"
1111
x:DataType="vm:MainViewModel"
12+
Loaded="Control_OnLoaded"
1213
mc:Ignorable="d">
1314
<Design.DataContext>
1415
<vm:MainViewModel />

llcomNext/LLCOM/Views/MainView.axaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
using System.Threading.Tasks;
12
using Avalonia;
23
using Avalonia.Controls;
4+
using Avalonia.Interactivity;
35
using Avalonia.Markup.Xaml;
6+
using LLCOM.ViewModels;
47

58
namespace LLCOM.Views;
69

@@ -10,4 +13,9 @@ public MainView()
1013
{
1114
InitializeComponent();
1215
}
16+
17+
private async void Control_OnLoaded(object? sender, RoutedEventArgs e)
18+
{
19+
await ((MainViewModel)this.DataContext!).PageLoadedCommand.ExecuteAsync(null);
20+
}
1321
}

0 commit comments

Comments
 (0)