实现基本的节点组装逻辑

This commit is contained in:
zengwenjie
2025-09-15 21:37:13 +08:00
parent a6e92b6704
commit cba82ca4c4
7 changed files with 136 additions and 7 deletions

View File

@@ -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)));
/// <summary>
/// 处理「DeedyActionViewer.DeedyAction」属性变更
/// </summary>
protected virtual void DeedyAction_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
protected virtual void DeedyAction_PropertyChangedCallback(DependencyPropertyChangedEventArgs e)
{
//
}
public void ReadyToWorking(Runtime? runtime)
{
throw new NotImplementedException();

View File

@@ -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();
}

View File

@@ -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
{
/// <summary>
/// 根据名称为一个对象的指定属性赋值
/// </summary>
/// <typeparam name="TValue">属性的数据类型</typeparam>
/// <param name="target">要操作的目标对象</param>
/// <param name="propertyName">属性名</param>
/// <param name="value">属性值</param>
/// <param name="bindingFlags">属性约束</param>
/// <exception cref="Exception">错误的原因</exception>
internal static void SetNamedPropertyValue<TValue>(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);
}
/// <summary>
/// 根据名称获取一个对象的指定属性的值
/// </summary>
/// <typeparam name="TValue">属性的数据类型</typeparam>
/// <param name="target">要操作的目标对象</param>
/// <param name="propertyName">属性名</param>
/// <param name="bindingFlags">属性约束</param>
/// <returns>属性值</returns>
/// <exception cref="Exception">错误的原因</exception>
internal static TValue? GetNamedPropertyValue<TValue>(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("对象上指定属性的当前值不是期望类型的有效实例...");
}
}
}

View File

@@ -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();
}

View File

@@ -8,5 +8,6 @@ namespace Deedy.Activity
{
public interface IDeedyLayout : IContainerElement
{
}
}

View File

@@ -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);
}
}

View File

@@ -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);
/// <summary>
/// 将当前节点链接到一个节点上
/// </summary>
/// <param name="element">要链接到的节点</param>
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);
}
/// <summary>
/// 断开自身与父级节点的链接关系
/// </summary>
public virtual void Unlink()
{
(this.ParentElement as IContainerElement)?.Remove(this);
this.SetNamedPropertyValue<IDeedyElement?>(nameof(ParentElement), null);
this.SetNamedPropertyValue(nameof(DepthLevel), 0);
}
/// <summary>
/// 构建结构树,设置层级:并且完成参数映射的刷新
/// </summary>
/// <param name="parent"></param>
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;
}
}
}