From cba82ca4c4040fa933f4a8fd0ae1c31272c38aed Mon Sep 17 00:00:00 2001 From: zengwenjie <1663900244@qq.com> Date: Mon, 15 Sep 2025 21:37:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9F=BA=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E7=BB=84=E8=A3=85=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DeedyDesigner/Deedy.Activity/ActionViewer.cs | 7 +- DeedyDesigner/Deedy.Activity/DeedyAction.cs | 3 +- DeedyDesigner/Deedy.Activity/DeedyHelper.cs | 40 +++++++++ .../Deedy.Activity/DeedyVisual/DeedyLayout.cs | 3 +- .../DeedyVisual/IDeedyLayout.cs | 1 + DeedyDesigner/Deedy.Activity/IActionViewer.cs | 2 + DeedyDesigner/Deedy.Activity/IDeedyElement.cs | 87 ++++++++++++++++++- 7 files changed, 136 insertions(+), 7 deletions(-) diff --git a/DeedyDesigner/Deedy.Activity/ActionViewer.cs b/DeedyDesigner/Deedy.Activity/ActionViewer.cs index 3b9a891..f818623 100644 --- a/DeedyDesigner/Deedy.Activity/ActionViewer.cs +++ b/DeedyDesigner/Deedy.Activity/ActionViewer.cs @@ -99,15 +99,14 @@ namespace Deedy.Activity } public static readonly DependencyProperty DeedyActionProperty = DependencyProperty.Register("DeedyAction", typeof(IDeedyAction), typeof(ActionViewer), new PropertyMetadata(null, - (d, e) => (d as ActionViewer)?.DeedyAction_PropertyChangedCallback(d, e))); + (d, e) => (d as ActionViewer)?.DeedyAction_PropertyChangedCallback(e))); /// /// 处理「DeedyActionViewer.DeedyAction」属性变更 /// - protected virtual void DeedyAction_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) + protected virtual void DeedyAction_PropertyChangedCallback(DependencyPropertyChangedEventArgs e) { - + // } - public void ReadyToWorking(Runtime? runtime) { throw new NotImplementedException(); diff --git a/DeedyDesigner/Deedy.Activity/DeedyAction.cs b/DeedyDesigner/Deedy.Activity/DeedyAction.cs index 66af921..5800f92 100644 --- a/DeedyDesigner/Deedy.Activity/DeedyAction.cs +++ b/DeedyDesigner/Deedy.Activity/DeedyAction.cs @@ -15,6 +15,7 @@ namespace Deedy.Activity public string DETitle { get; set; } = ""; public string DERemark { get; set; } = ""; public string DEIdentify { get; set; } = ""; + public int DepthLevel { get; protected internal set; } = 0; public IDeedyElement? ParentElement { get; protected internal set; } public IActionViewer? ActionViewer { get; protected internal set; } @@ -69,7 +70,7 @@ namespace Deedy.Activity throw new NotImplementedException(); } - public void ReadyToWorking(Runtime? runtime) + public void ReadyToWorking(IDeedyElement? parent = null) { throw new NotImplementedException(); } diff --git a/DeedyDesigner/Deedy.Activity/DeedyHelper.cs b/DeedyDesigner/Deedy.Activity/DeedyHelper.cs index 0628840..5e505ba 100644 --- a/DeedyDesigner/Deedy.Activity/DeedyHelper.cs +++ b/DeedyDesigner/Deedy.Activity/DeedyHelper.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -8,5 +9,44 @@ namespace Deedy.Activity { public static class DeedyHelper { + /// + /// 根据名称为一个对象的指定属性赋值 + /// + /// 属性的数据类型 + /// 要操作的目标对象 + /// 属性名 + /// 属性值 + /// 属性约束 + /// 错误的原因 + internal static void SetNamedPropertyValue(this object target, string propertyName, TValue value, + BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.SetProperty) + { + if (target == null || string.IsNullOrEmpty(propertyName)) throw new Exception("目标对象与属性名可能为空,无法对其赋值..."); + Type type = target.GetType(); + PropertyInfo? property = type.GetProperty(propertyName, bindingFlags); + if (property == null) throw new Exception("目标对象上找不到指定名称的可赋值属性..."); + if (value != null && !property.PropertyType.IsInstanceOfType(value)) throw new Exception("要赋予目标对象指定属性的值不匹配属性类型..."); + property.SetValue(target, value); + } + /// + /// 根据名称获取一个对象的指定属性的值 + /// + /// 属性的数据类型 + /// 要操作的目标对象 + /// 属性名 + /// 属性约束 + /// 属性值 + /// 错误的原因 + internal static TValue? GetNamedPropertyValue(this object target, string propertyName, + BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.GetProperty) + { + if (target == null || string.IsNullOrEmpty(propertyName)) throw new Exception("目标对象与属性名可能为空,无法完成取值..."); + Type type = target.GetType(); + PropertyInfo? property = type.GetProperty(propertyName, bindingFlags); + if (property == null) throw new Exception("目标对象上找不到指定名称的可取值属性..."); + object? value = property.GetValue(target); + if (typeof(TValue).IsInstanceOfType(value)) return (TValue?)value; + throw new Exception("对象上指定属性的当前值不是期望类型的有效实例..."); + } } } diff --git a/DeedyDesigner/Deedy.Activity/DeedyVisual/DeedyLayout.cs b/DeedyDesigner/Deedy.Activity/DeedyVisual/DeedyLayout.cs index 4fe42a8..bc63a3d 100644 --- a/DeedyDesigner/Deedy.Activity/DeedyVisual/DeedyLayout.cs +++ b/DeedyDesigner/Deedy.Activity/DeedyVisual/DeedyLayout.cs @@ -14,6 +14,7 @@ namespace Deedy.Activity public string DETitle { get; set; } = ""; public string DERemark { get; set; } = ""; public string DEIdentify { get; set; } = ""; + public int DepthLevel { get; protected internal set; } = 0; public IDeedyElement? ParentElement { get; protected internal set; } public IDeedyElement RootElement => (this.ParentElement == null) ? this : this.ParentElement.RootElement; @@ -86,7 +87,7 @@ namespace Deedy.Activity throw new NotImplementedException(); } - public void ReadyToWorking(Runtime? runtime) + public void ReadyToWorking(IDeedyElement? parent = null) { throw new NotImplementedException(); } diff --git a/DeedyDesigner/Deedy.Activity/DeedyVisual/IDeedyLayout.cs b/DeedyDesigner/Deedy.Activity/DeedyVisual/IDeedyLayout.cs index 8ddae95..5d0911c 100644 --- a/DeedyDesigner/Deedy.Activity/DeedyVisual/IDeedyLayout.cs +++ b/DeedyDesigner/Deedy.Activity/DeedyVisual/IDeedyLayout.cs @@ -8,5 +8,6 @@ namespace Deedy.Activity { public interface IDeedyLayout : IContainerElement { + } } diff --git a/DeedyDesigner/Deedy.Activity/IActionViewer.cs b/DeedyDesigner/Deedy.Activity/IActionViewer.cs index af2d4b9..fdeec36 100644 --- a/DeedyDesigner/Deedy.Activity/IActionViewer.cs +++ b/DeedyDesigner/Deedy.Activity/IActionViewer.cs @@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows; namespace Deedy.Activity { @@ -13,5 +14,6 @@ namespace Deedy.Activity [AllowNull] public IDeedyAction DeedyAction { get; set; } public void ReadyToWorking(Runtime? runtime); + } } diff --git a/DeedyDesigner/Deedy.Activity/IDeedyElement.cs b/DeedyDesigner/Deedy.Activity/IDeedyElement.cs index 3e6941a..7ac21f0 100644 --- a/DeedyDesigner/Deedy.Activity/IDeedyElement.cs +++ b/DeedyDesigner/Deedy.Activity/IDeedyElement.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; +using System.Windows.Markup; namespace Deedy.Activity { @@ -14,11 +16,94 @@ namespace Deedy.Activity public string DETitle { get; set; } public string DERemark { get; set; } public string DEIdentify { get; set; } + public int DepthLevel { get; } public IDeedyElement? ParentElement { get; } public IDeedyElement RootElement { get; } public void InsertToFore(IDeedyElement deedyElement); public void InsertAtRear(IDeedyElement deedyElement); public void SwapIndex(IDeedyElement deedyElement); - public void ReadyToWorking(Runtime? runtime); + /// + /// 将当前节点链接到一个节点上 + /// + /// 要链接到的节点 + public virtual void LinkTo([AllowNull] IDeedyElement element) + { + if (element == null) + { + this.Unlink(); + return; + } + if (this.ParentElement != null) + { + if (this.ParentElement == element) return; + else + { + if (this.ParentElement is IContainerElement container) + { + container.Remove(this); + container.Append(this); + this.SetNamedPropertyValue(nameof(ParentElement), element); + } + } + } + else + { + this.SetNamedPropertyValue(nameof(ParentElement), element); + } + this.SetNamedPropertyValue(nameof(DepthLevel), (this.ParentElement?.DepthLevel ?? 0) + 1); + } + /// + /// 断开自身与父级节点的链接关系 + /// + public virtual void Unlink() + { + (this.ParentElement as IContainerElement)?.Remove(this); + this.SetNamedPropertyValue(nameof(ParentElement), null); + this.SetNamedPropertyValue(nameof(DepthLevel), 0); + } + /// + /// 构建结构树,设置层级:并且完成参数映射的刷新 + /// + /// + public void ReadyToWorking(IDeedyElement? parent = null) + { + //TODO:刷新参数映射 + this.LinkTo(parent); + if (this is IContainerElement container) + foreach (IDeedyElement element in container.Elements) + element.ReadyToWorking(this); + } + public virtual IDeedyElement? Clone() + { + try + { + return XamlReader.Parse(XamlWriter.Save(this)) as IDeedyElement; + } + catch { return null; } + } + public virtual bool TryEncode(out string document) + { + bool result = false; + try + { + document = XamlWriter.Save(this); + result = true; + } + catch { document = string.Empty; } + return result; + } + public static virtual bool TryDecode(string document, out IDeedyElement? element) + { + IDeedyElement? instance = null; + bool result = false; + try + { + instance = XamlReader.Parse(document) as IDeedyElement; + result = instance != null; + } + catch { } + element = instance; + return result; + } } }