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 SelectedDevices { get; private set; } = new List(); private ObservableCollection _results = new ObservableCollection(); private List _brands; public SearchDeviceWindow() { InitializeComponent(); UiScaler.Attach(this); // 分辨率/DPI 自适应 ResultListBox.ItemsSource = _results; try { _brands = new List(); 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; } } }