From 095b84a6e6aad9fbe083536cd6defeb9d9078165 Mon Sep 17 00:00:00 2001 From: fyf <11621@LAPTOP-04QQU0AO> Date: Sat, 17 Sep 2022 12:00:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomerControls/TheRadioButton.cs | 2 +- .../Converters/BrowsableAttribute.cs | 93 +++++++++++++++ BeDesignerSCADA/ViewModel/MainViewModel.cs | 109 ++++++++++-------- 3 files changed, 152 insertions(+), 52 deletions(-) create mode 100644 BeDesignerSCADA/Converters/BrowsableAttribute.cs diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheRadioButton.cs b/BPASmartClient.SCADAControl/CustomerControls/TheRadioButton.cs index c56d26ed..cecfc87e 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheRadioButton.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheRadioButton.cs @@ -26,7 +26,7 @@ namespace BPASmartClient.SCADAControl.CustomerControls ResourceDictionary languageResDic = new ResourceDictionary(); languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute); this.Resources.MergedDictionaries.Add(languageResDic); - SetCurrentValue(ContentProperty, "单选按钮"); + //SetCurrentValue(ContentProperty, "单选按钮"); } static TheRadioButton() { diff --git a/BeDesignerSCADA/Converters/BrowsableAttribute.cs b/BeDesignerSCADA/Converters/BrowsableAttribute.cs new file mode 100644 index 00000000..84fc8481 --- /dev/null +++ b/BeDesignerSCADA/Converters/BrowsableAttribute.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BeDesignerSCADA.Converters +{ + // + // 摘要: + // Specifies whether a property or event should be displayed in a Properties window. + [AttributeUsage(AttributeTargets.All)] + public class BrowsableAttribute :Attribute + { + // + // 摘要: + // Specifies the default value for the System.ComponentModel.BrowsableAttribute, + // which is System.ComponentModel.BrowsableAttribute.Yes. This static field is read-only. + public static BrowsableAttribute Default; + + // + // 摘要: + // Specifies that a property or event cannot be modified at design time. This static + // field is read-only. + public static BrowsableAttribute No; + + // + // 摘要: + // Specifies that a property or event can be modified at design time. This static + // field is read-only. + public static BrowsableAttribute Yes; + + // + // 摘要: + // Gets a value indicating whether an object is browsable. + // + // 返回结果: + // true if the object is browsable; otherwise, false. + public bool Browsable { get; set; } + + // + // 摘要: + // Initializes a new instance of the System.ComponentModel.BrowsableAttribute class. + // + // 参数: + // browsable: + // true if a property or event can be modified at design time; otherwise, false. + // The default is true. + public BrowsableAttribute(bool browsable) + { + this.Browsable = browsable; + } + + // + // 摘要: + // Indicates whether this instance and a specified object are equal. + // + // 参数: + // obj: + // Another object to compare to. + // + // 返回结果: + // true if obj is equal to this instance; otherwise, false. + public override bool Equals([NotNullWhen(true)] object? obj) + { + throw null; + } + + // + // 摘要: + // Returns the hash code for this instance. + // + // 返回结果: + // A 32-bit signed integer hash code. + public override int GetHashCode() + { + throw null; + } + + // + // 摘要: + // Determines if this attribute is the default. + // + // 返回结果: + // true if the attribute is the default value for this attribute class; otherwise, + // false. + public override bool IsDefaultAttribute() + { + throw null; + } + } +} diff --git a/BeDesignerSCADA/ViewModel/MainViewModel.cs b/BeDesignerSCADA/ViewModel/MainViewModel.cs index ad44dede..077eec96 100644 --- a/BeDesignerSCADA/ViewModel/MainViewModel.cs +++ b/BeDesignerSCADA/ViewModel/MainViewModel.cs @@ -274,55 +274,52 @@ namespace BeDesignerSCADA.ViewModel { "Code" , "代码过滤脚本"}, { "GenerateData" , "数据结果"}, }; - //{"EventSendNameList" , "发送事件集"} /// /// 数据刷新 /// public void DataSX() { - //if (CanSelectedItem is System.Windows.Controls.Control) - { - //属性变量 - PropertyGridCommand cmd = new PropertyGridCommand(); - var content = CanSelectedItem;// as System.Windows.Controls.Control; - if (content is IExecutable executable) - executable.PropertyChange += Executable_PropertyChange; + //属性变量 + PropertyGridCommand cmd = new PropertyGridCommand(); + var content = CanSelectedItem;// as System.Windows.Controls.Control; + if (content is IExecutable executable) + executable.PropertyChange += Executable_PropertyChange; - foreach (var item in EventName) + foreach (var item in EventName) + { + System.Reflection.PropertyInfo info = content.GetType().GetProperty(item.Key); + var propName = info?.GetValue(content,null); + if (info != null && (item.Key == "Content" ? (propName is string) : true)) { - System.Reflection.PropertyInfo info = content.GetType().GetProperty(item.Key); - var propName = info?.GetValue(content,null); - if (info != null && item.Key == "Content" ? (propName is string) : true) - { - //var taget = this.GetType().GetMembers().Where(t => t.Name.Equals("BrowsableTrue")).FirstOrDefault(); - //var datas= CustomAttributeData.GetCustomAttributes(taget)[0]; - //cmd.GetType().GetProperty(item.Value).CustomAttributes.Append(datas); - cmd.GetType().GetProperty(item.Value).SetValue(cmd,propName); - } + SetPropertyVisibility(cmd,item.Value,true); + cmd.GetType().GetProperty(item.Value).SetValue(cmd,propName); + } + else + { + SetPropertyVisibility(cmd,item.Value,false); } - cmd.PropertyChanged += Cmd_PropertyChanged; - PropeObject = cmd; } + cmd.PropertyChanged += Cmd_PropertyChanged; + PropeObject = null; + PropeObject = cmd; } + /// + /// 内部属性变化通知 + /// + /// + /// private void Executable_PropertyChange(object? sender,EventArgs e) { System.Windows.Controls.Control content = CanSelectedItem as System.Windows.Controls.Control; System.Reflection.PropertyInfo info = content.GetType().GetProperty("GenerateData"); var propName = info?.GetValue(content,null); PropeObject.GetType().GetProperty("数据结果").SetValue(PropeObject,propName); - - DevNameList = new System.Collections.ObjectModel.ObservableCollection(); DevValueList = new System.Collections.ObjectModel.ObservableCollection(); Class_DataBus.GetInstance().Dic_DeviceData.Keys?.ToList().ForEach(key => { DevNameList.Add(key); }); } - [Browsable(true)] - public void BrowsableTrue() { } - [Browsable(false)] - public void BrowsableFalse() { } - /// /// 修改属性后 /// @@ -377,36 +374,46 @@ namespace BeDesignerSCADA.ViewModel } #endregion - #region 设置 + #region 特性控制 /// - /// 设置Browsable特性的值 + /// 通过反射控制属性是否可见 /// /// /// /// - public void SetPropertyVisibility(object obj,string propertyName,bool visible) + public static void SetPropertyVisibility(object obj,string propertyName,bool visible) { - //Type type = typeof(BrowsableAttribute); - //PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj); - //AttributeCollection attrs = props[propertyName].Attributes; - //type.GetProperty("Browsable").SetValue(attrs[type], visible); - - //AttributeCollection attributes = - // TypeDescriptor.GetProperties(obj)[propertyName].Attributes; - //// Checks to see if the property is browsable. - //BrowsableAttribute myAttribute = (BrowsableAttribute)attributes[typeof( - //BrowsableAttribute)]; - - //if (myAttribute.Browsable) - //{ - // // Insert code here. - //} - - //PropertyDescriptorCollection appSetingAttributes = TypeDescriptor.GetProperties(obj); - //Type type = typeof(BrowsableAttribute); - //FieldInfo fieldInfo = type.GetField("_displayName", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance); - //fieldInfo.SetValue(appSetingAttributes["TextID"].Attributes[displayType], "修改后的文本ID"); + Type type = typeof(BrowsableAttribute); + PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj); + AttributeCollection attrs = props[propertyName].Attributes; + FieldInfo fld = CallPrivatevariables(type,"k__BackingField"); + // FieldInfo fld = type.GetField("Browsable",BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreReturn | BindingFlags.GetProperty); + fld.SetValue(attrs[type],visible); + } + /// + /// 获取指定对象的指定成员变量 + /// + /// + /// + /// + /// + public static FieldInfo CallPrivatevariables(Type type,string name) + { + //BindingFlags flag = System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.NonPublic; + //Type type = obj.GetType(); + //var fieldInfos_ = type.GetField(name, flag); + var fieldInfos = (System.Reflection.FieldInfo[])((System.Reflection.TypeInfo)type).DeclaredFields; + FieldInfo fieldInfo_ = null; + foreach (FieldInfo fieldInfo in fieldInfos) + { + if (fieldInfo.Name == name) + { + return fieldInfo;// (T)fieldInfo.GetValue(obj); + } + } + //FieldInfo fieldInfo = type.GetField(name, flag); + return fieldInfo_;// (T)fieldInfo_.GetValue(obj); } /// /// 设置Category特性的值 @@ -477,7 +484,7 @@ namespace BeDesignerSCADA.ViewModel private string _文本1; private object _变量; - [Category("基本属性"), Description("Name"), Browsable(true), PropertyOrder(1)] + [Category("基本属性"), Description("Name"), Browsable(false), PropertyOrder(1)] public string 名称 { get