SearchDeviceWindow.xaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using TeamAAS.Camera;
  7. using TeamAAS.Camera.Models;
  8. using TeamAAS.Dialogs;
  9. namespace TeamAAS.Views
  10. {
  11. public partial class SearchDeviceWindow
  12. {
  13. public List<CameraInfo> SelectedDevices { get; private set; } = new List<CameraInfo>();
  14. private ObservableCollection<CameraInfo> _results = new ObservableCollection<CameraInfo>();
  15. private List<BrandOption> _brands;
  16. public SearchDeviceWindow()
  17. {
  18. InitializeComponent();
  19. UiScaler.Attach(this); // 分辨率/DPI 自适应
  20. ResultListBox.ItemsSource = _results;
  21. try
  22. {
  23. _brands = new List<BrandOption>();
  24. var rawBrands = CameraManager.Instance.GetAvailableBrands();
  25. foreach (var (Brand, DisplayName, Installed) in rawBrands)
  26. {
  27. _brands.Add(new BrandOption { Brand = Brand, DisplayName = DisplayName, Installed = Installed });
  28. }
  29. BrandComboBox.ItemsSource = _brands;
  30. foreach (var b in _brands)
  31. {
  32. if (b.Installed)
  33. {
  34. BrandComboBox.SelectedValue = b.Brand;
  35. break;
  36. }
  37. }
  38. }
  39. catch (Exception ex)
  40. {
  41. StatusText.Text = $"加载品牌列表失败: {ex.Message}";
  42. }
  43. }
  44. private async void Search_Click(object sender, RoutedEventArgs e)
  45. {
  46. if (BrandComboBox.SelectedValue == null)
  47. {
  48. StatusText.Text = "请先选择相机品牌";
  49. return;
  50. }
  51. var brand = (string)BrandComboBox.SelectedValue;
  52. var brandInfo = _brands.Find(b => b.Brand == brand);
  53. if (!brandInfo.Installed)
  54. {
  55. StatusText.Text = $"⚠ {brandInfo.DisplayName} SDK 未安装,无法搜索";
  56. return;
  57. }
  58. LoadingIndicator.Visibility = Visibility.Visible;
  59. StatusText.Text = $"正在搜索 {brandInfo.DisplayName} 设备...";
  60. _results.Clear();
  61. AddButton.IsEnabled = false;
  62. try
  63. {
  64. var devices = await Task.Run(() => CameraManager.Instance.DiscoverByBrand(brand));
  65. foreach (var d in devices)
  66. {
  67. _results.Add(d);
  68. }
  69. StatusText.Text = $"搜索完成,找到 {devices.Count} 个设备";
  70. }
  71. catch (Exception ex)
  72. {
  73. StatusText.Text = $"搜索失败: {ex.Message}";
  74. }
  75. finally
  76. {
  77. LoadingIndicator.Visibility = Visibility.Collapsed;
  78. }
  79. }
  80. private void Result_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  81. {
  82. AddButton.IsEnabled = ResultListBox.SelectedItem != null;
  83. }
  84. private void Add_Click(object sender, RoutedEventArgs e)
  85. {
  86. var selected = ResultListBox.SelectedItem as CameraInfo;
  87. if (selected == null) return;
  88. var info = selected.Copy();
  89. info.Id = Guid.NewGuid();
  90. if (string.IsNullOrEmpty(info.CameraName))
  91. {
  92. info.CameraName = $"{CameraManager.Instance.GetBrandDisplayName(info.CameraBrand)}-{info.SerialNumber}";
  93. }
  94. SelectedDevices.Add(info);
  95. _results.Remove(selected);
  96. this.Close();
  97. }
  98. private void Close_Click(object sender, RoutedEventArgs e)
  99. {
  100. Close();
  101. }
  102. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  103. {
  104. DialogResult = SelectedDevices.Count > 0;
  105. }
  106. }
  107. public class BrandOption
  108. {
  109. public string Brand { get; set; }
  110. public string DisplayName { get; set; }
  111. public bool Installed { get; set; }
  112. }
  113. }