Browse Source

其他-补充深拷贝,修复右键菜单串联问题

ZhouJieSheng 1 year ago
parent
commit
b16ef2dbda

+ 50 - 0
PDF Office/Helper/CommonHelper.cs

@@ -8,6 +8,7 @@ using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Runtime.InteropServices;
+using System.Runtime.Serialization.Formatters.Binary;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading.Tasks;
@@ -538,6 +539,55 @@ namespace PDF_Master.Helper
                 return text;
             }
             catch { return ""; }
+        }
+
+        /// <summary>
+        /// 深拷贝方法 解决全局的静态右键菜单问题
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="obj"></param>
+        /// <returns></returns>
+        public static T DeepClone<T>(T obj)
+        {
+            if (obj == null)
+            {
+                return default(T);
+            }
+
+            var objType = obj.GetType();
+            if (objType.IsValueType || objType == typeof(string))
+            {
+                return obj;
+            }
+
+            // 创建新实例
+            T newObj = (T)Activator.CreateInstance(objType);
+
+            // 递归复制属性
+            PropertyInfo[] properties = objType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
+            foreach (PropertyInfo property in properties)
+            {
+                if (!property.CanWrite || !property.CanRead)
+                {
+                    continue;
+                }
+                // 如果属性是引用类型,继续拷贝
+                var propertyType = property.PropertyType;
+                if (propertyType.IsClass && propertyType != typeof(string))
+                {
+                    var propertyValue = property.GetValue(obj, null);
+                    var propertyValueClone = DeepClone(propertyValue);
+                    property.SetValue(newObj, propertyValueClone, null);
+                }
+                // 否则直接赋值
+                else
+                {
+                    var propertyValue = property.GetValue(obj, null);
+                    property.SetValue(newObj, propertyValue, null);
+                }
+            }
+
+            return newObj;
         }
     }
 }

+ 5 - 1
PDF Office/ViewModels/Tools/AnnotToolContentViewModel.Layout.cs

@@ -6,6 +6,7 @@ using PDF_Master.CustomControl;
 using PDF_Master.CustomControl.CompositeControl;
 using PDF_Master.Helper;
 using PDF_Master.Model.PropertyPanel.AnnotPanel;
+using Prism.Commands;
 using Prism.Mvvm;
 using Prism.Regions;
 using System;
@@ -18,6 +19,7 @@ using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Input;
+using System.Windows.Markup;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Shapes;
@@ -125,7 +127,9 @@ namespace PDF_Master.ViewModels.Tools
 
         private ContextMenu ViewerContextMenu(object sender)
         {
-            ContextMenu contextMenu = App.Current.FindResource("ViewerContextMenu") as ContextMenu;
+            //ContextMenu contextMenu = CommonHelper.DeepClone(App.Current.FindResource("ViewerContextMenu") as ContextMenu);
+            string xaml = XamlWriter.Save(App.Current.FindResource("ViewerContextMenu") as ContextMenu);
+            ContextMenu contextMenu = XamlReader.Parse(xaml) as ContextMenu;
             //contextMenu.Loaded += ContextMenu_Loaded;
             ViewerContextMenu_Loaded(contextMenu, sender);
             return contextMenu;