From e57938fbda9bff702004038f76d009422968bff5 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期五, 06 十一月 2026 18:05:07 +0800
Subject: [PATCH] 提交代码

---
 DeviceService/AddDeviceWindow.xaml.cs |   97 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 97 insertions(+), 0 deletions(-)

diff --git a/DeviceService/AddDeviceWindow.xaml.cs b/DeviceService/AddDeviceWindow.xaml.cs
new file mode 100644
index 0000000..78777da
--- /dev/null
+++ b/DeviceService/AddDeviceWindow.xaml.cs
@@ -0,0 +1,97 @@
+using DeviceService.Core;
+using DeviceService.Device;
+using System.Windows;
+using System.Windows.Controls;
+
+namespace DeviceService;
+
+/// <summary>
+/// 娣诲姞璁惧寮圭獥锛氫粠 AllDeviceType 涓�夋嫨璁惧绫诲瀷锛�
+/// 閫夊瀷鍚庡湪涓嬫柟鍔犺浇璇ヨ澶囩被鍨嬪搴旂殑閰嶇疆 UserControl锛岀‘瀹氬悗浜ゅ洖璁惧瀹炰緥銆�
+/// </summary>
+public partial class AddDeviceWindow : Window
+{
+    private IDevice? _device;
+
+    /// <summary>鐐瑰嚮纭畾鍚庤繑鍥炲緟娣诲姞鐨勮澶囧疄渚嬶紱鍙栨秷鏃朵负 null銆�</summary>
+    public IDevice? Device { get; private set; }
+
+    public AddDeviceWindow()
+    {
+        InitializeComponent();
+        TypeCombo.ItemsSource = DeviceHelper.AllDeviceType;
+    }
+
+    private void TypeCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
+    {
+        _device = null;
+        ControlHost.Content = null;
+
+        if (TypeCombo.SelectedItem is string type
+            && DeviceHelper.TryCreateDevice(type, out IDevice? device)
+            && device != null)
+        {
+            // 榛樿 Id锛氱被鍨嬪悕-搴忓彿锛屽彲鍦ㄩ厤缃帶浠朵腑鐪嬪埌
+            int seq = DeviceManager.Devices.Count(d => d.Type == type) + 1;
+            SetDeviceId(device, $"{type}-{seq}");
+            _device = device;
+
+            UserControl? control = DeviceConfigControlFactory.Create(device);
+            if (control == null)
+            {
+                ControlHost.Content = new TextBlock
+                {
+                    Text = $"鏈壘鍒� {device.GetType().Name}ConfigControl锛岃璁惧绫诲瀷鏆傛棤閰嶇疆鐣岄潰銆�",
+                    Margin = new Thickness(8),
+                    TextWrapping = TextWrapping.Wrap,
+                    TextAlignment = TextAlignment.Center,
+                    VerticalAlignment = VerticalAlignment.Center
+                };
+            }
+            else
+            {
+                control.DataContext = device;
+                ControlHost.Content = control;
+            }
+        }
+
+        OkButton.IsEnabled = _device != null;
+    }
+
+    private void OkButton_Click(object sender, RoutedEventArgs e)
+    {
+        if (_device == null)
+        {
+            return;
+        }
+
+        string id = _device.DeviceId;
+        if (string.IsNullOrWhiteSpace(id))
+        {
+            MessageBox.Show(this, "璁惧 ID 涓嶈兘涓虹┖銆�", "娣诲姞璁惧",
+                MessageBoxButton.OK, MessageBoxImage.Warning);
+            return;
+        }
+
+        if (DeviceManager.Devices.Any(d => d.DeviceId == id))
+        {
+            MessageBox.Show(this, $"璁惧 ID \"{id}\" 宸插瓨鍦紝璇蜂慨鏀瑰悗閲嶈瘯銆�", "娣诲姞璁惧",
+                MessageBoxButton.OK, MessageBoxImage.Warning);
+            return;
+        }
+
+        Device = _device;
+        DialogResult = true;
+    }
+
+    private void CancelButton_Click(object sender, RoutedEventArgs e)
+    {
+        DialogResult = false;
+    }
+
+    /// <summary>IDevice 鎺ュ彛鍙毚闇� Id getter锛屽疄闄呭疄鐜扮被涓�鑸湁 setter锛岄�氳繃鍙嶅皠璧嬪�笺��</summary>
+    private static void SetDeviceId(IDevice device, string id)
+    {
+        device.GetType().GetProperty(nameof(IDevice.DeviceId))?.SetValue(device, id);
+    }
+}

--
Gitblit v1.9.1