diff --git a/DeedyDesigner/Deedy.Activity/Attribute/ParamDefineAttribute.cs b/DeedyDesigner/Deedy.Activity/Attribute/ParamDefineAttribute.cs index c53f89f..3fc4b11 100644 --- a/DeedyDesigner/Deedy.Activity/Attribute/ParamDefineAttribute.cs +++ b/DeedyDesigner/Deedy.Activity/Attribute/ParamDefineAttribute.cs @@ -1,20 +1,64 @@ using System; using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace Deedy.Activity { [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] - public class ParamDefineAttribute : Attribute, IInitialSortable + public class ParamDefineAttribute : Attribute, IInitialSortable, INotifyPropertyChanged { - public int InitialSort { get; set; } = 0; public ParamDefineAttribute() { } + public event PropertyChangedEventHandler? PropertyChanged; + #region 定义属性通知事件相关方法 + //public event PropertyChangedEventHandler? PropertyChanged; + protected virtual void OnPropertyChanged([CallerMemberName, AllowNull] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + protected virtual T GetField(ref T field, [CallerMemberName, AllowNull] string propertyName = null) + { + return field; + } + protected virtual bool SetField(ref T field, T value, [CallerMemberName, AllowNull] string propertyName = null) + { + if (EqualityComparer.Default.Equals(field, value)) return false; + field = value; + OnPropertyChanged(propertyName); + return true; + } + #endregion + + public int InitialSort { get; set; } = 0; + public ParamDefineAttribute(string title, string remark, string @default) { } + public string Name { get; set; } = ""; public string Title { get; set; } = "参数名称"; public string Remark { get; set; } = "参数描述"; + public string Identify { get; set; } = ""; public string Default { get; set; } = ""; - public string DesignValue { get; set; } = ""; - public string GroupName { get; set; } = ""; + protected internal string _DesignValue = ""; + /// + /// 设计期值 + /// + public string DesignValue + { + get { return GetField(ref _DesignValue); } + set + { + if (SetField(ref _DesignValue, value)) + { + // 这里可以加入属性变更后的处理逻辑 + } + } + } + public string GroupName { get; set; } = ParamGroupAttribute.DefaultParams; + + public object? Target { get; protected internal set; } + public PropertyInfo? Property { get; protected internal set; } } } diff --git a/DeedyDesigner/Deedy.Activity/Collection/ParamDefineCollection.cs b/DeedyDesigner/Deedy.Activity/Collection/ParamDefineCollection.cs index c8962ba..2553531 100644 --- a/DeedyDesigner/Deedy.Activity/Collection/ParamDefineCollection.cs +++ b/DeedyDesigner/Deedy.Activity/Collection/ParamDefineCollection.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -16,5 +17,23 @@ namespace Deedy.Activity { private readonly SortedList _SoredList = new(); internal ParamDefineCollection() { } + public bool TryGetParamDefine(string name, [NotNull] out ParamDefineAttribute value) + { + ParamDefineAttribute pda = new(); + bool result = false; + + foreach (var item in this) + { + if (item.Name == name) + { + pda = item; + result = true; + break; + } + } + + value = pda; + return result; + } } } diff --git a/DeedyDesigner/Deedy.Activity/Helpers/ElementHelper.cs b/DeedyDesigner/Deedy.Activity/Helpers/ElementHelper.cs index af163e5..c47fc1b 100644 --- a/DeedyDesigner/Deedy.Activity/Helpers/ElementHelper.cs +++ b/DeedyDesigner/Deedy.Activity/Helpers/ElementHelper.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; @@ -98,9 +99,24 @@ namespace Deedy.Activity /// 消息输出器 public static void Help_BuildParamMapping(this IElement element, Output? output = null) { - //TODO:构建元素的参数映射表 if (element == null) return; if (element.ParamsMapping == null) element.ParamsMapping = new(); + var props = element.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty); + foreach (var prop in props) + { + if (prop.CanWrite && prop.CanRead) + { + var pda = prop.GetCustomAttribute(); + if (pda != null) + { + pda.Name = prop.Name; + if (element.ParamsMapping.TryGetParamDefine(prop.Name, out var value)) pda = value; + else element.ParamsMapping.Add(pda); + value.Target = element; + value.Property = prop; + } + } + } } /// /// 构建一个元素的参数分组,为设计器准备的辅助方法