-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavItemSend.xaml.cs
More file actions
716 lines (640 loc) · 26.5 KB
/
Copy pathNavItemSend.xaml.cs
File metadata and controls
716 lines (640 loc) · 26.5 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
using LocalDrop.Sender;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Devices.Enumeration;
using Windows.Devices.WiFiDirect;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Pickers;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace LocalDrop
{
public class DeviceInfo
{
public required string DeviceName { get; set; }
public required string DeviceId { get; set; }
}
public static class FileTypeToIconConverter
{
public static Symbol Convert(FileType fileType)
{
return fileType switch
{
FileType.TEXT => Symbol.Document,
FileType.IMG => Symbol.Pictures,
FileType.FILE => Symbol.Document,
FileType.AUDIO => Symbol.Audio,
FileType.VIDEO => Symbol.Video,
FileType.DIR => Symbol.Folder,
FileType.QUICK_MESSAGE => Symbol.Message,
_ => Symbol.Document
};
}
}
public static class FileSizeConverter
{
public static string Convert(long bytes)
{
string[] sizes = { "B", "KB", "MB", "GB" };
int order = 0;
while (bytes >= 1024 && order < sizes.Length - 1)
{
order++;
bytes /= 1024;
}
return $"{bytes:0.##} {sizes[order]}";
}
}
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class NavItemSend : Page
{
private ObservableCollection<SendTransferItem> ActiveSendTransfers { get; } = new();
private double _totalProgress = 0;
private string _totalProgressText = "";
private int port = int.Parse(MySettings.ReadJsonToDictionary()["port"].ToString());
public double TotalProgress
{
get => _totalProgress;
set => SetProperty(ref _totalProgress, value);
}
public string TotalProgressText
{
get => _totalProgressText;
set => SetProperty(ref _totalProgressText, value);
}
ObservableCollection<DeviceInfo> deviceInfoes = new ObservableCollection<DeviceInfo>();
ObservableCollection<FileInfo> fileInfoes = new ObservableCollection<FileInfo>();
WiFiDirectAdvertisementPublisher _publisher = new WiFiDirectAdvertisementPublisher();
DeviceWatcher? _deviceWatcher;
bool _fWatcherStarted = false;
public NavItemSend()
{
this.InitializeComponent();
deviceInfoes.Clear();
NearbyDevice.ItemsSource = deviceInfoes;
WaitSendFileListView.DragOver += OnDragOver;
WaitSendFileListView.Drop += OnDrop;
BeginScanner();
}
private async void OnDragOver(object sender, DragEventArgs e)
{
// 检查是否包含可接受的文件类型
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
// 获取拖动的文件信息
var deferral = e.GetDeferral();
try
{
var items = await e.DataView.GetStorageItemsAsync();
if (items.Count > 0)
{
e.AcceptedOperation = DataPackageOperation.Copy;
e.DragUIOverride.Caption = "添加文件到发送列表";
e.DragUIOverride.IsGlyphVisible = true;
}
}
finally
{
deferral.Complete();
}
}
}
private async void OnDrop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var deferral = e.GetDeferral();
try
{
var items = await e.DataView.GetStorageItemsAsync();
foreach (var item in items)
{
if (item is StorageFile file)
{
var properties = await file.GetBasicPropertiesAsync();
long fileSize = (long)properties.Size;
// 确保在UI线程更新集合
DispatcherQueue.TryEnqueue(() =>
{
fileInfoes.Add(new FileInfo()
{
fileName = file.Name,
fileSize = fileSize,
fileType = FileType.FILE,
info = file.Path
});
});
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"拖放文件错误: {ex.Message}");
await ShowMessageDialog($"无法添加文件: {ex.Message}");
}
finally
{
deferral.Complete();
}
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
CleanupResources();
base.OnNavigatedFrom(e);
}
private void CleanupResources()
{
Debug.WriteLine("开始清理发送资源");
// 1. 停止设备扫描和广播
if (_deviceWatcher != null)
{
StopWatcher(); // 调用现有的停止方法
}
if (_publisher.Status == WiFiDirectAdvertisementPublisherStatus.Started)
{
_publisher.Stop();
}
// 2. 断开WiFiDirect连接
Disconnect();
ActiveSendTransfers.Clear();
// 3. 清理文件列表
fileInfoes.Clear();
}
private async void WaitSendFileListView_ItemClick(object sender, ItemClickEventArgs e)
{
var selectedFile = e.ClickedItem as FileInfo;
if (selectedFile == null) return;
ContentDialog deleteDialog = new ContentDialog
{
Title = "删除确认",
Content = $"确定要删除 {selectedFile.fileName} 吗?",
PrimaryButtonText = "删除",
SecondaryButtonText = "查看",
CloseButtonText = "取消"
};
deleteDialog.XamlRoot = this.Content.XamlRoot;
var result = await deleteDialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
fileInfoes.Remove(selectedFile);
}
else if (result == ContentDialogResult.Secondary)
{
Process p = new Process();
p.StartInfo.FileName = "explorer.exe";
p.StartInfo.Arguments = selectedFile.info;
p.Start();
}
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
if (button == SendFile)
{
var window = new Microsoft.UI.Xaml.Window();
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
FileOpenPicker files = new FileOpenPicker();
files.SuggestedStartLocation = PickerLocationId.Desktop;
files.FileTypeFilter.Add("*");
WinRT.Interop.InitializeWithWindow.Initialize(files, hwnd);
var selectedFiles = await files.PickMultipleFilesAsync();
foreach (var selectedFile in selectedFiles)
{
var properties = await selectedFile.GetBasicPropertiesAsync();
long fileSize = (long)properties.Size;
fileInfoes.Add(new FileInfo()
{
fileName = selectedFile.Name,
fileSize = fileSize,
fileType = FileType.FILE,
info = selectedFile.Path,
});
Debug.WriteLine($"添加文件{selectedFile.Name},大小{fileSize},地址{selectedFile.Path}");
}
}
else if (button == ReScanner)
{
deviceInfoes.Clear();
}
else if (button == SendText)
{
//todo 弹窗输入文本
var dialog = new ContentDialog
{
Title = "文本",
Content = new TextBox { AcceptsReturn = true },
PrimaryButtonText = "发送",
CloseButtonText = "取消",
XamlRoot = this.Content.XamlRoot
};
if (await dialog.ShowAsync() == ContentDialogResult.Primary)
{
var textBox = dialog.Content as TextBox;
var the_text = textBox.Text;
fileInfoes.Add(new FileInfo()
{
fileName = "QUICK_MESSAGE.txt",
fileSize = the_text.Length,
fileType = FileType.QUICK_MESSAGE,
info = the_text,
});
Debug.WriteLine($"添加文本:{the_text}");
}
}
else if (button == SendCut)
{
try
{
DataPackageView dataPackageView = Clipboard.GetContent();
bool contentAdded = false;
// 处理剪切板文件
if (dataPackageView.Contains(StandardDataFormats.StorageItems))
{
IReadOnlyList<IStorageItem> items = await dataPackageView.GetStorageItemsAsync();
foreach (var item in items)
{
if (item is StorageFile file)
{
var properties = await file.GetBasicPropertiesAsync();
long fileSize = (long)properties.Size;
fileInfoes.Add(new FileInfo()
{
fileName = file.Name,
fileSize = fileSize,
fileType = FileType.FILE,
info = file.Path,
});
Debug.WriteLine($"添加剪切板文件:{file.Name}, 地址:{file.Path}");
contentAdded = true;
}
}
}
// 处理剪切板文本
if (dataPackageView.Contains(StandardDataFormats.Text))
{
string textContent = await dataPackageView.GetTextAsync();
if (!string.IsNullOrWhiteSpace(textContent))
{
fileInfoes.Add(new FileInfo()
{
fileName = "QUICK_MESSAGE.txt",
fileSize = textContent.Length,
fileType = FileType.QUICK_MESSAGE,
info = textContent,
});
Debug.WriteLine($"添加剪切板文本:{textContent}");
contentAdded = true;
}
}
// 无内容提示
if (!contentAdded)
{
await ShowMessageDialog("剪切板中未找到文件或文本内容");
}
}
catch (Exception ex)
{
Debug.WriteLine($"剪切板操作异常: {ex.Message}");
await ShowMessageDialog($"操作失败: {ex.Message}");
}
}
}
}
private async Task ShowMessageDialog(string message)
{
ContentDialog dialog = new ContentDialog()
{
Title = "提示",
Content = message,
CloseButtonText = "确定",
XamlRoot = this.Content.XamlRoot // 确保对话框正确显示
};
await dialog.ShowAsync();
}
private void BeginScanner()
{
if (_deviceWatcher == null)
{
Debug.WriteLine("开始广播");
_publisher.StatusChanged += PublicStatusChanged;
_publisher.Start();
}
else
{
_publisher.Stop();
StopWatcher();
Debug.WriteLine("停止扫描");
}
}
private async void PublicStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs args)
{
if (args.Status == WiFiDirectAdvertisementPublisherStatus.Started)
{
//AssociationEndpoint 关联的终结点。 这包括其他电脑、平板电脑和手机。
//DeviceInterface 设备接口。
WiFiDirectDeviceSelectorType wiFiDirectDeviceSelectorType = WiFiDirectDeviceSelectorType.AssociationEndpoint;
string deviceSelector = WiFiDirectDevice.GetDeviceSelector(wiFiDirectDeviceSelectorType);
_deviceWatcher = DeviceInformation.CreateWatcher(
deviceSelector,
new string[] { "System.Devices.WiFiDirect.InformationElements" }
);
_deviceWatcher.Added += OnDeviceAdded;
_deviceWatcher.Removed += OnDeviceRemoved;
_deviceWatcher.Updated += OnDeviceUpdated;
_deviceWatcher.EnumerationCompleted += OnEnumerationCompleted;
_deviceWatcher.Stopped += OnStopped;
_deviceWatcher.Start();
_fWatcherStarted = true;
}
else
{
Debug.WriteLine($"wifiDirect状态{args.Status}");
}
}
private void NearbyDevice_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (NearbyDevice.SelectedItem is DeviceInfo selectedDevice)
{
System.Diagnostics.Debug.WriteLine($"选中设备:{selectedDevice.DeviceName} (ID: {selectedDevice.DeviceId})");
if (fileInfoes.Count != 0)
{
_ = Connect(selectedDevice.DeviceId);
NearbyDevice.SelectedIndex = -1;
}
else
{
//todo 添加弹窗 请选择要发送的文件
DispatcherQueue.TryEnqueue(() =>
{
ContentDialog dialog = new ContentDialog
{
Title = "文件未选择",
Content = "请先选择要发送的文件",
CloseButtonText = "确定",
XamlRoot = this.Content.XamlRoot // 确保在WinUI中正确设置XamlRoot
};
_ = dialog.ShowAsync();
NearbyDevice.SelectedIndex = -1;
});
}
}
}
private void StopWatcher()
{
_deviceWatcher.Added -= OnDeviceAdded;
_deviceWatcher.Removed -= OnDeviceRemoved;
_deviceWatcher.Updated -= OnDeviceUpdated;
_deviceWatcher.EnumerationCompleted -= OnEnumerationCompleted;
_deviceWatcher.Stopped -= OnStopped;
_deviceWatcher.Stop();
_deviceWatcher = null;
}
//#region DeviceWatcherEvents
private void OnDeviceAdded(DeviceWatcher deviceWatcher, DeviceInformation deviceInfo)
{
DispatcherQueue.TryEnqueue(() =>
{
var name = deviceInfo.Name;
var id = deviceInfo.Id;
deviceInfoes.Add(new DeviceInfo()
{
DeviceId = id,
DeviceName = name,
});
Debug.WriteLine($"成功添加设备:{name} ({id})");
});
}
private void OnDeviceRemoved(
DeviceWatcher deviceWatcher,
DeviceInformationUpdate deviceInfoUpdate
)
{
}
private void OnDeviceUpdated(
DeviceWatcher deviceWatcher,
DeviceInformationUpdate deviceInfoUpdate
)
{
}
private void OnEnumerationCompleted(DeviceWatcher deviceWatcher, object o)
{
}
private void OnStopped(DeviceWatcher deviceWatcher, object o)
{
}
//#endregion
Windows.Devices.WiFiDirect.WiFiDirectDevice? wfdDevice;
private async System.Threading.Tasks.Task<String> Connect(string deviceId)
{
string result = "";
Debug.WriteLine("开始连接并发送文件");
try
{
// No device ID specified.
if (String.IsNullOrEmpty(deviceId))
{
Debug.WriteLine("Please specify a Wi-Fi Direct device ID.");
return "Please specify a Wi-Fi Direct device ID.";
}
// Connect to the selected Wi-Fi Direct device.
wfdDevice = await Windows.Devices.WiFiDirect.WiFiDirectDevice.FromIdAsync(deviceId);
if (wfdDevice == null)
{
result = "Connection to " + deviceId + " failed.";
Debug.WriteLine("Connection to " + deviceId + " failed.");
}
// Register for connection status change notification.
wfdDevice.ConnectionStatusChanged += new TypedEventHandler<Windows.Devices.WiFiDirect.WiFiDirectDevice, object>(OnConnectionChanged);
// Get the EndpointPair information.
var EndpointPairCollection = wfdDevice.GetConnectionEndpointPairs();
if (EndpointPairCollection.Count > 0)
{
var endpointPair = EndpointPairCollection[0];
if (fileInfoes.Count != 0)
{
FileSender fileSender = new FileSender();
fileSender.ProgressChanged += OnSendProgressChanged;
fileSender.TransferCompleted += OnSendCompleted;
fileSender.DispatcherQueue = DispatcherQueue;
// 初始化发送任务
var transfers = fileInfoes.Select(f => new SendTransferItem
{
FileName = f.fileName,
FilePath = f.info,
FileSize = f.fileSize,
Status = TransferSendingStatus.Pending
}).ToList();
foreach (var transfer in transfers)
{
ActiveSendTransfers.Add(transfer);
}
// 显示进度弹窗
DispatcherQueue.TryEnqueue(() =>
{
_ = SendProgressDialog.ShowAsync();
});
UpdateTotalProgress();
// 开始发送(异步)
_ = Task.Run(async () =>
{
foreach (var (fileInfo, transfer) in fileInfoes.Zip(transfers, (f, t) => (f, t)))
{
try
{
DispatcherQueue.TryEnqueue(() =>
{
transfer.Status = TransferSendingStatus.Sending;
});
await fileSender.SendFileAsync(fileInfo,
endpointPair.RemoteHostName.ToString(),
port,
transfer);
DispatcherQueue.TryEnqueue(() =>
{
fileInfoes.Remove(fileInfo);
transfer.Status = TransferSendingStatus.Completed;
});
}
catch (Exception ex)
{
DispatcherQueue.TryEnqueue(() =>
{
transfer.Status = TransferSendingStatus.Failed;
});
Debug.WriteLine($"发送失败: {ex.Message}");
}
finally
{
UpdateTotalProgress();
}
}
});
}
//var endpointPair = EndpointPairCollection[0];
//result = "Local IP address " + endpointPair.LocalHostName.ToString() +
// " connected to remote IP address " + endpointPair.RemoteHostName.ToString();
//if (fileInfoes.Count != 0)
//{
// FileSender fileSender = new FileSender();
// foreach (var fileInfo in fileInfoes)
// {
// DispatcherQueue.TryEnqueue(() =>
// {
// NowSendFileText.Text = $"正在发送{fileInfo.fileName}";
// });
// Debug.WriteLine($"开始发送{fileInfo.fileName}");
// fileSender.SendFile(fileInfo, endpointPair.RemoteHostName.ToString(), 27431);
// DispatcherQueue.TryEnqueue(() =>
// {
// NowSendFileText.Text = $"传输完成{fileInfo.fileName},等待发送下一个文件。";
// fileInfoes.Remove(fileInfo);
// });
// }
// DispatcherQueue.TryEnqueue(() =>
// {
// NowSendFileText.Text = $"发送列表";
// });
//}
}
else
{
result = "Connection to " + deviceId + " failed.";
}
}
catch (Exception err)
{
// Handle error.
result = "Error occurred: " + err.Message;
}
Debug.WriteLine(result);
return result;
}
private void OnSendProgressChanged(SendTransferItem transferItem)
{
UpdateTotalProgress();
}
private void OnSendCompleted(SendTransferItem transferItem)
{
DispatcherQueue.TryEnqueue(() =>
{
if (ActiveSendTransfers.All(t => t.Status == TransferSendingStatus.Completed ||
t.Status == TransferSendingStatus.Failed))
{
SendProgressDialog.Hide();
}
});
}
private void UpdateTotalProgress()
{
DispatcherQueue.TryEnqueue(() =>
{
if (ActiveSendTransfers.Count == 0) return;
var totalSize = ActiveSendTransfers.Sum(t => t.FileSize);
var sentSize = ActiveSendTransfers.Sum(t => t.BytesSent);
TotalProgress = (sentSize / (double)totalSize) * 100;
TotalProgressText = $"{TotalProgress:0.0}% ({sentSize:N0}/{totalSize:N0} bytes)";
});
}
private void OnSendDialogClosing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
if (ActiveSendTransfers.Any(t => t.Status == TransferSendingStatus.Sending))
{
args.Cancel = true;
SendProgressDialog.Hide();
}
}
// 添加INotifyPropertyChanged支持
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
private void OnConnectionChanged(object sender, object arg)
{
if (arg != null)
{
Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus status =
(Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus)arg;
if (status == Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus.Connected)
{
// Connection successful.
Debug.WriteLine($"Connected: {status}");
}
else
{
// Disconnected.
Disconnect();
}
}
}
private void Disconnect()
{
if (wfdDevice != null)
{
wfdDevice.Dispose();
}
}
}
}