Files
DeedyDesigner/DeedyDesigner/Deedy.Activity/IElement.cs
2025-09-18 15:37:28 +08:00

113 lines
3.8 KiB
C#

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
{
public interface IElement : IActivity, INotifyPropertyChanged
{
public string Class { get; }
public string Title { get; set; }
public string Remark { get; set; }
public string Identify { get; set; }
public int DepthLevel { get; }
public IElement? ParentElement { get; }
public IElement RootElement { get; }
/// <summary>
/// 将当前节点链接到一个节点上
/// </summary>
/// <param name="element">要链接到的节点</param>
public virtual void LinkTo([AllowNull] IElement 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);
this.Setter_ParentElement(element);
}
}
}
else
{
//this.SetNamedPropertyValue(nameof(ParentElement), element);
this.Setter_ParentElement(element);
}
//this.SetNamedPropertyValue(nameof(DepthLevel), (this.ParentElement?.DepthLevel ?? 0) + 1);
this.Setter_DepthLevel((this.ParentElement?.DepthLevel ?? 0) + 1);
}
/// <summary>
/// 断开自身与父级节点的链接关系
/// </summary>
public virtual void Unlink()
{
(this.ParentElement as IContainerElement)?.Remove(this);
//this.SetNamedPropertyValue<IElement?>(nameof(ParentElement), null);
this.Setter_ParentElement(null);
//this.SetNamedPropertyValue(nameof(DepthLevel), 0);
this.Setter_DepthLevel(0);
}
/// <summary>
/// 构建结构树,设置层级:并且完成参数映射的刷新
/// </summary>
/// <param name="parent"></param>
public new void ReadyToWorking(IElement? element = null, Output? output = null)
{
this.BuildParamMapping(output);
this.LinkTo(element);
if (this is IContainerElement container)
foreach (IElement subElement in container.Elements)
subElement.ReadyToWorking(this);
}
public virtual IElement? Clone()
{
try
{
return XamlReader.Parse(XamlWriter.Save(this)) as IElement;
}
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 IElement? element)
{
IElement? instance = null;
bool result = false;
try
{
instance = XamlReader.Parse(document) as IElement;
result = instance != null;
}
catch { }
element = instance;
return result;
}
}
}