From 12e606635c42ea20f4b7c2bdda5204237a4b4c96 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期一, 02 三月 2026 16:14:49 +0800
Subject: [PATCH] 优化

---
 WPFProject/Controls/MainTabControl.xaml.cs |   77 +++++++++++++++++++++++++++++++-------
 1 files changed, 62 insertions(+), 15 deletions(-)

diff --git a/WPFProject/Controls/MainTabControl.xaml.cs b/WPFProject/Controls/MainTabControl.xaml.cs
index 1e57271..a476ebd 100644
--- a/WPFProject/Controls/MainTabControl.xaml.cs
+++ b/WPFProject/Controls/MainTabControl.xaml.cs
@@ -1,16 +1,13 @@
 锘縰sing HMI.Base;
 using HMI.Core.Api;
-using Microsoft.DwayneNeed.Interop;
 using Panuon.WPF.UI;
+using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Runtime.CompilerServices;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
-using System.Windows.Forms.Integration;
-using System.Windows.Media;
-using WPFProject.UI;
 
 namespace WPFProject.Controls
 {
@@ -41,7 +38,6 @@
         private WinFormHolder winFormHolder;
         public MainTabControl()
         {
-            IPlugin plugin = new WorkControl();
             MenuItems = new ObservableCollection<PluginInfo>(PluginLoader.GetLoader().Menus);
             SubMenuItems = MenuItems[0].Children;
             this.DataContext = this;
@@ -94,6 +90,7 @@
             }
         }
 
+        private Dictionary<PluginInfo, PluginInfo> selectedCache = new Dictionary<PluginInfo, PluginInfo>();
 
         private ObservableCollection<PluginInfo> _SubMenuItems;
 
@@ -138,23 +135,48 @@
             {
                 var data = (PluginInfo)e.AddedItems[0];
                 SubMenuItems = data?.Children;
+                if (selectedCache.ContainsKey(data))
+                {
+                    SelectedSubMenu = selectedCache[data];
+                }
+                else if (SubMenuItems.Count > 0)
+                {
+                    SelectedSubMenu = SubMenuItems[0];
+                }
             }
         }
-
-        private void SwithMenu(PluginInfo menu)
-        {
-
-        }
-
 
         private void SubMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
             if (e.AddedItems?.Count > 0)
             {
                 var data = (PluginInfo)e.AddedItems[0];
+                if (SelectedMenu != null)
+                {
+                    selectedCache[SelectedMenu] = data;
+                }
+
                 var content = data.Content;
-                var last = page.Child;
-                PluginPackage.Get(last)?.DeActive();
+                var newPlugin = PluginPackage.Get(content);
+                var last = PluginPackage.Get(page.Child);
+
+                if (last != null && !last.CanDeActive())
+                {
+                    SelectedSubMenu = (PluginInfo)e.RemovedItems[0];
+                    return;
+                }
+
+                if (newPlugin != null && !newPlugin.CanActive())
+                {
+                    if (e.RemovedItems.Count > 0)
+                    {
+                        SelectedSubMenu = (PluginInfo)e.RemovedItems[0];
+                    }
+                    return;
+                }
+
+                last?.DeActive();
+                newPlugin?.Active();
                 if (data.Type == "WinForm")
                 {
                     if (content is System.Windows.Forms.Form form)
@@ -171,8 +193,6 @@
                 {
                     page.Child = (UIElement)content;
                 }
-
-                PluginPackage.Get(content)?.Active();
             }
         }
 
@@ -186,6 +206,33 @@
                 UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
             };
             winFormHolder.SetBinding(WinFormHolder.MaskVisiableProperty, binding);
+            if (MenuItems != null && MenuItems.Count > 0)
+            {
+                SelectedMenu = MenuItems[0];
+            }
+            Loaded -= UserControl_Loaded;
+        }
+
+        private string alarmInfo;
+        public string AlarmInfo
+        {
+            get => alarmInfo;
+            set
+            {
+                RaiseAndSetIfChanged(ref alarmInfo, value);
+            }
+        }
+
+        private void Button_Click_1(object sender, RoutedEventArgs e)
+        {
+            alarmBorder.Visibility = Visibility.Collapsed;
+            AlarmInfo = string.Empty;
+        }
+
+        public void ShowAlarm(string message)
+        {
+            AlarmInfo = message;
+            alarmBorder.Visibility = Visibility.Visible;
         }
     }
 }

--
Gitblit v1.9.1