-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathIndexPage.xaml.cs
398 lines (281 loc) · 12.8 KB
/
IndexPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SQLite;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IPAM_NOTE.DatabaseOperation;
using MaterialDesignThemes.Wpf;
using static IPAM_NOTE.ViewMode;
namespace IPAM_NOTE.UserPages
{
/// <summary>
/// IndexPage.xaml 的交互逻辑
/// </summary>
public partial class IndexPage : UserControl
{
public IndexPage()
{
InitializeComponent();
}
private DbClass dbClass;
private void IndexPage_OnLoaded(object sender, RoutedEventArgs e)
{
string dbFilePath = AppDomain.CurrentDomain.BaseDirectory + @"db\";
string dbName = "Address_database.db";
dbFilePath = dbFilePath + dbName;
dbClass = new DbClass(dbFilePath);
dbClass.OpenConnection();
//加载所有的设备名称,表名,型号,编号
LoadDevicesInfo(dbClass.connection);
if (DataBrige.DeviceInfos != null)
{
int index = 0;
foreach (var info in DataBrige.DeviceInfos)//查询所有的表
{
string tableName = info.TableName;
////查询每个设备对应的标签
string sql = $"SELECT * FROM {tableName} WHERE PortType ='I' and PortStatus = 1";
SQLiteCommand command = new SQLiteCommand(sql, dbClass.connection);
SQLiteDataReader reader = command.ExecuteReader();
int rowCount = 0;
while (reader.Read())
{
rowCount++;
}
if (rowCount != 0)
{
index += 1;
Grid grid = new Grid();
string name = info.Name;
string model = info.Model;
string number = info.Number;
string note = info.Description;
// 设置 Grid 行定义
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); // 最小高度为 20
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
// 设置 Grid 列定义
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(40) }); // 左边宽度为 40
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(260) }); // 左边宽度为 260
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); // 右边宽度自适应
//索引
TextBlock indexTextBlock = new TextBlock();
indexTextBlock.Text = index.ToString();
indexTextBlock.Style = (Style)this.FindResource("MaterialDesignHeadline6TextBlock");
indexTextBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
indexTextBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
indexTextBlock.FontWeight = FontWeights.Black;
Grid.SetColumn(indexTextBlock, 0); // 设置左边 TextBlock 的列位置
Grid.SetRow(indexTextBlock, 0); // 设置左边 TextBlock 的行位置
Grid.SetRowSpan(indexTextBlock, 2);
StackPanel stack = new StackPanel();
stack.Orientation = Orientation.Horizontal;
Grid.SetColumn(stack, 1); // 设置左边 TextBlock 的列位置
Grid.SetRow(stack, 0); // 设置左边 TextBlock 的行位置
TextBlock nameTextBlock = new TextBlock();
nameTextBlock.Text = name;
nameTextBlock.Style = (Style)this.FindResource("MaterialDesignHeadline6TextBlock");
nameTextBlock.Width = 80;
nameTextBlock.Margin = new Thickness(0);
nameTextBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
nameTextBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
TextBlock nameTextBlock2 = new TextBlock();
nameTextBlock2.Text = number;
nameTextBlock2.Style = (Style)this.FindResource("MaterialDesignHeadline6TextBlock");
nameTextBlock2.Width = 220;
nameTextBlock2.Margin = new Thickness(0);
nameTextBlock2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
nameTextBlock2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
stack.Children.Add(nameTextBlock);
stack.Children.Add(nameTextBlock2);
stack.VerticalAlignment = VerticalAlignment.Bottom;
StackPanel stack2 = new StackPanel();
stack2.VerticalAlignment = VerticalAlignment.Top;
stack2.HorizontalAlignment = HorizontalAlignment.Left;
stack2.Orientation = Orientation.Horizontal;
Grid.SetColumn(stack2, 1); // 设置左边 TextBlock 的列位置
Grid.SetRow(stack2, 1); // 设置左边 TextBlock 的行位置
//型号信息
TextBlock modelTextBlock = new TextBlock();
modelTextBlock.Text = model;
modelTextBlock.Width = 150;
modelTextBlock.Style = (Style)this.FindResource("MaterialDesignCaptionTextBlock");
modelTextBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
modelTextBlock.HorizontalAlignment = HorizontalAlignment.Left;
modelTextBlock.Opacity = 0.3;
//注释信息
TextBlock noteBlock = new TextBlock();
noteBlock.Text = note;
noteBlock.Style = (Style)this.FindResource("MaterialDesignCaptionTextBlock");
noteBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
noteBlock.HorizontalAlignment = HorizontalAlignment.Left;
noteBlock.Opacity = 0.3;
stack2.Children.Add(modelTextBlock);
stack2.Children.Add(noteBlock);
WrapPanel chiPanel = new WrapPanel();
Grid.SetColumn(chiPanel, 2); // 设置左边 TextBlock 的列位置
Grid.SetRow(chiPanel, 0); // 设置左边 TextBlock 的行位置
Grid.SetRowSpan(chiPanel, 2);
grid.Children.Add(indexTextBlock);//索引
grid.Children.Add(stack);//名称+设备编号
grid.Children.Add(stack2);//型号+注释
grid.Children.Add(chiPanel);//快速访问标签
////查询每个设备对应的标签
string sql2 = $"SELECT * FROM {tableName} WHERE PortType ='I' and PortStatus = 1";
SQLiteCommand command2 = new SQLiteCommand(sql, dbClass.connection);
reader = command2.ExecuteReader();
while (reader.Read())
{
string portTag1 = reader["PortTag1"].ToString();
string portTag2 = reader["PortTag2"].ToString();
//Console.WriteLine(portTag1);
Button button = new Button();
button.Height = 60;
button.Width = 130;
button.Content = CreatButtonPanel(portTag1, portTag2);
button.BorderThickness = new Thickness(0);
button.Margin = new Thickness(5);
button.Click += Button_Click;
button.Tag = portTag2;
button.ToolTip ="标签名称:"+ portTag1 + "\r访问地址:" + portTag2;
button.Background = Brushes.DarkCyan;
button.Foreground = (Brush)FindResource("MaterialDesignDarkForeground");
chiPanel.Children.Add(button);
}
Graphics.Children.Add(grid);
Separator separator = new Separator();
separator.Width = 10000; // 设置横线的宽度,根据需要调整
separator.Opacity = 0.3;
Graphics.Children.Add(separator);
}
}
}
}
/// <summary>
/// 点击按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
string url = button.Tag.ToString();
string pattern = @"\[\d+\]";
url = Regex.Replace(url, pattern, "");
if (!url.Contains("https://") && !url.Contains("http://"))
{
// 如果字符串中不存在 "https://" 或 "http://", 添加 "https://"
url = "http://" + url;
}
Process.Start(url);
}
}
/// <summary>
/// 创建一个Button的样式
/// </summary>
/// <returns></returns>
private Grid CreatButtonPanel(string name,string url)
{
Grid grid = new Grid();
ColumnDefinition column1 = new ColumnDefinition();
ColumnDefinition column2 = new ColumnDefinition();
RowDefinition row1 = new RowDefinition();
RowDefinition row2 = new RowDefinition();
column1.Width = new GridLength(20);
column2.Width = new GridLength(100);
row1.Height = new GridLength(60, GridUnitType.Star);
row2.Height = new GridLength(40, GridUnitType.Star);
grid.ColumnDefinitions.Add(column1);
grid.ColumnDefinitions.Add(column2);
grid.RowDefinitions.Add(row1);
grid.RowDefinitions.Add(row2);
grid.VerticalAlignment = VerticalAlignment.Center;
// 添加PackIcon到第一列
PackIcon packIcon = new PackIcon();
packIcon.Kind = PackIconKind.GoogleChrome; // 设置PackIcon的图标类型
packIcon.Margin = new Thickness(0);
packIcon.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(packIcon, 0);
Grid.SetRow(packIcon, 0);
Grid.SetRowSpan(packIcon, 2);
TextBlock textBlock1 = new TextBlock();
textBlock1.Text = name;
textBlock1.FontWeight = FontWeights.Bold;
textBlock1.FontSize = 16;
textBlock1.Foreground= (Brush)FindResource("MaterialDesignDarkForeground");
TextBlock textBlock2 = new TextBlock();
textBlock2.Text = url;
textBlock2.FontWeight = FontWeights.Bold;
textBlock2.FontSize = 12;
textBlock2.Opacity = 0.5;
textBlock2.Foreground = (Brush)FindResource("MaterialDesignDarkForeground");
Grid.SetColumn(textBlock1, 1);
Grid.SetRow(textBlock1, 0);
Grid.SetColumn(textBlock2, 1);
Grid.SetRow(textBlock2, 1);
grid.Children.Add(packIcon);
grid.Children.Add(textBlock1);
grid.Children.Add(textBlock2);
return grid;
}
/// <summary>
/// 加载设备信息列表
/// </summary>
/// <param name="connection"></param>
public void LoadDevicesInfo(SQLiteConnection connection)
{
DataBrige.DeviceInfos.Clear();
Graphics.Children.Clear(); //
try
{
string query = "SELECT * FROM Devices";
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataReader reader = command.ExecuteReader();
int i = 0;
while (reader.Read())
{
i++;
// 读取数据行中的每一列
int id = i;
string tableName = reader["TableName"].ToString();
string name = reader["Name"].ToString();
string model = reader["Model"].ToString();
string number = reader["Number"].ToString();
string people = reader["People"].ToString();
string date = reader["Date"].ToString();
string description = reader["Description"].ToString();
int ePort = Convert.ToInt32(reader["Eport"]);
string ePortTag = reader["EportTag"].ToString();
int fPort = Convert.ToInt32(reader["Fport"]);
string fPortTag = reader["FportTag"].ToString();
int mPort = Convert.ToInt32(reader["Mport"]);
string mPortTag = reader["MportTag"].ToString();
int dPort = Convert.ToInt32(reader["Mport"]);
string dPortTag = reader["DportTag"].ToString();
DataBrige.DeviceInfos.Add(new DeviceInfo(id, tableName, name, model, number, people, date,
description, ePort, ePortTag, fPort, fPortTag, mPort, mPortTag, dPort, dPortTag));
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}