| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Threading.Tasks;
- using System.Windows;
- using TeamAAS.Camera;
- using TeamAAS.Camera.Models;
- using TeamAAS.Dialogs;
- namespace TeamAAS.Views
- {
- public partial class SearchDeviceWindow
- {
- public List<CameraInfo> SelectedDevices { get; private set; } = new List<CameraInfo>();
- private ObservableCollection<CameraInfo> _results = new ObservableCollection<CameraInfo>();
- private List<BrandOption> _brands;
- public SearchDeviceWindow()
- {
- InitializeComponent();
- UiScaler.Attach(this); // 分辨率/DPI 自适应
- ResultListBox.ItemsSource = _results;
- try
- {
- _brands = new List<BrandOption>();
- var rawBrands = CameraManager.Instance.GetAvailableBrands();
- foreach (var (Brand, DisplayName, Installed) in rawBrands)
- {
- _brands.Add(new BrandOption { Brand = Brand, DisplayName = DisplayName, Installed = Installed });
- }
- BrandComboBox.ItemsSource = _brands;
- foreach (var b in _brands)
- {
- if (b.Installed)
- {
- BrandComboBox.SelectedValue = b.Brand;
- break;
- }
- }
- }
- catch (Exception ex)
- {
- StatusText.Text = $"加载品牌列表失败: {ex.Message}";
- }
- }
- private async void Search_Click(object sender, RoutedEventArgs e)
- {
- if (BrandComboBox.SelectedValue == null)
- {
- StatusText.Text = "请先选择相机品牌";
- return;
- }
- var brand = (string)BrandComboBox.SelectedValue;
- var brandInfo = _brands.Find(b => b.Brand == brand);
- if (!brandInfo.Installed)
- {
- StatusText.Text = $"⚠ {brandInfo.DisplayName} SDK 未安装,无法搜索";
- return;
- }
- LoadingIndicator.Visibility = Visibility.Visible;
- StatusText.Text = $"正在搜索 {brandInfo.DisplayName} 设备...";
- _results.Clear();
- AddButton.IsEnabled = false;
- try
- {
- var devices = await Task.Run(() => CameraManager.Instance.DiscoverByBrand(brand));
- foreach (var d in devices)
- {
- _results.Add(d);
- }
- StatusText.Text = $"搜索完成,找到 {devices.Count} 个设备";
- }
- catch (Exception ex)
- {
- StatusText.Text = $"搜索失败: {ex.Message}";
- }
- finally
- {
- LoadingIndicator.Visibility = Visibility.Collapsed;
- }
- }
- private void Result_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- AddButton.IsEnabled = ResultListBox.SelectedItem != null;
- }
- private void Add_Click(object sender, RoutedEventArgs e)
- {
- var selected = ResultListBox.SelectedItem as CameraInfo;
- if (selected == null) return;
- var info = selected.Copy();
- info.Id = Guid.NewGuid();
- if (string.IsNullOrEmpty(info.CameraName))
- {
- info.CameraName = $"{CameraManager.Instance.GetBrandDisplayName(info.CameraBrand)}-{info.SerialNumber}";
- }
- SelectedDevices.Add(info);
- _results.Remove(selected);
- this.Close();
- }
- private void Close_Click(object sender, RoutedEventArgs e)
- {
- Close();
- }
- private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- DialogResult = SelectedDevices.Count > 0;
- }
- }
- public class BrandOption
- {
- public string Brand { get; set; }
- public string DisplayName { get; set; }
- public bool Installed { get; set; }
- }
- }
|