using BeDesignerSCADA.Common; using BeDesignerSCADA.Controls; using BeDesignerSCADA.View; using BPASmartClient.Compiler; using BPASmartClient.DATABUS; using BPASmartClient.MessageName; using BPASmartClient.MessageName.EnumHelp; using BPASmartClient.MessageName.发送消息Model; using BPASmartClient.MessageName.接收消息Model; using ICSharpCode.AvalonEdit; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using Xceed.Wpf.Toolkit.PropertyGrid.Attributes; namespace BeDesignerSCADA.ViewModel { /// /// 主函数 ViewModel /// public class MainViewModel :ObservableObject { public MainViewModel() { IsRunning = false; //启动 或者 停止 RunUiCommand = new RelayCommand(() => { IsRunning = !IsRunning; }); } #region 变量 /// /// 是否正在运行状态 /// private bool _IsRunning = false; public bool IsRunning { get { return _IsRunning; } set { _IsRunning = value; if (value) { RunCanvasVisibility = Visibility.Visible; CanvasPanelVisibility = Visibility.Hidden; } else { RunCanvasVisibility = Visibility.Hidden; CanvasPanelVisibility = Visibility.Visible; } OnPropertyChanged("IsRunning"); } } /// /// 画布是否显示 /// private Visibility _CanvasPanelVisibility; public Visibility CanvasPanelVisibility { get { return _CanvasPanelVisibility; } set { if (_CanvasPanelVisibility == value) return; _CanvasPanelVisibility = value; OnPropertyChanged("CanvasPanelVisibility"); } } /// /// 运行代码是否显示 /// private Visibility _RunCanvasVisibility; public Visibility RunCanvasVisibility { get { return _RunCanvasVisibility; } set { if (_RunCanvasVisibility == value) return; _RunCanvasVisibility = value; OnPropertyChanged("RunCanvasVisibility"); } } /// /// 当前代码Xaml /// private string _XamlCode; public string XamlCode { get { return _XamlCode; } set { if (_XamlCode == value) return; _XamlCode = value; OnPropertyChanged("XamlCode"); } } /// /// 选中控件 /// private FrameworkElement _CanSelectedItem; public FrameworkElement CanSelectedItem { get { return _CanSelectedItem; } set { if (_CanSelectedItem == value) return; _CanSelectedItem = value; SelectedPropertyDataRefresh(); OnPropertyChanged("CanSelectedItem"); } } /// /// 控制协议属性 /// private PropertyGridCommand _pro; public PropertyGridCommand PropeObject { get { return _pro; } set { _pro = value; OnPropertyChanged("PropeObject"); } } /// /// 设备名称集合 /// private ObservableCollection _DevNameList; public ObservableCollection DevNameList { get { return _DevNameList; } set { _DevNameList = value; OnPropertyChanged("DevNameList"); } } /// /// 设备变量集合 /// private ObservableCollection _DevValueList; public ObservableCollection DevValueList { get { return _DevValueList; } set { _DevValueList = value; OnPropertyChanged("DevValueList"); } } #endregion #region 命令 /// /// 启动或者停止 /// public RelayCommand RunUiCommand { get; set; } /// /// 编辑 /// public RelayCommand EditCommand { get; set; } #endregion #region 常用函数 /// /// 显示当前xaml代码 /// public void ShowCode(TextEditor textEditor) { textEditor.Text = canvasPanel.Save(); } #endregion #region 脚本编辑数据 /// /// 当前编辑界面 /// public CanvasPanel canvasPanel; /// /// 当前运行界面 /// public RunCanvas runCanvas; /// /// 加载 /// /// public void Loaded(object objCan,object objRun) { canvasPanel = objCan as CanvasPanel; runCanvas = objRun as RunCanvas; } /// /// 编辑 /// /// public void Edit(object obj) { string result = JsEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString(),canvasPanel.GetAllExecChildren()); ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = result; } #endregion #region 图片选择路径 public void SelectPath(object obj) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp"; if (ofd.ShowDialog() == true) { //((DevExpress.Xpf.Editors.EditableDataObject)obj).Value = ofd.FileName; } } #endregion #region 属性填充 /// /// 选中属性数据刷新 /// public void SelectedPropertyDataRefresh() { //属性变量 PropertyGridCommand cmd = new PropertyGridCommand(); var content = CanSelectedItem; if (content is IExecutable executable) executable.PropertyChange += Executable_PropertyChange; 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)) { SetPropertyVisibility(cmd,item.Value,true); cmd.GetType().GetProperty(item.Value).SetValue(cmd,propName); } else { SetPropertyVisibility(cmd,item.Value,false); } } 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); }); } /// /// 修改属性后 /// /// /// private void Cmd_PropertyChanged(object? sender,PropertyChangedEventArgs e) { try { string name = e.PropertyName; foreach (var item in EventName) { if (item.Value == name) { name = item.Key; } } PropertyGridCommand hotinfo = (PropertyGridCommand)sender; System.Reflection.PropertyInfo info = hotinfo.GetType().GetProperty(e.PropertyName); var propName = info?.GetValue(hotinfo,null); ProGridSetValue(name,propName); } catch (Exception ex) { } } /// /// 设置变量 /// /// public void ProGridSetValue(string Name,object value) { try { var content = CanSelectedItem; if (Name == "Content" ? (content is BPASmartClient.SCADAControl.CustomerControls.TheButton) : true) { System.Reflection.PropertyInfo info = content.GetType().GetProperty(Name); if (info != null) { if (info.ToString().Contains("Double")) { info?.SetValue(content, Double.Parse(value.ToString()), null); } else if (info.ToString().Contains("String")) { info?.SetValue(content, value, null); } else if (info.ToString().Contains("Int")) { info?.SetValue(content, int.Parse(value.ToString()), null); } else { info?.SetValue(content, value, null); } } } } catch (Exception ex) { } } #endregion #region 特性控制 /// /// 通过反射控制属性是否可见 /// /// /// /// public static void SetPropertyVisibility(object obj,string propertyName,bool visible) { 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特性的值 /// /// /// /// public void SetCategoryValue(object obj,string propertyName,string str) { Type type = typeof(CategoryAttribute); PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj); AttributeCollection attrs = props[propertyName].Attributes; FieldInfo fld = type.GetField("categoryValue",BindingFlags.Instance | BindingFlags.NonPublic); fld?.SetValue(attrs[type],str); } /// /// 获取类的属性成员的Category特性的值 /// /// /// /// public string GetCategoryValue(object obj,string propertyName) { Type type = typeof(CategoryAttribute); PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj); AttributeCollection attrs = props[propertyName].Attributes; FieldInfo fld = type.GetField("categoryValue",BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); var str = fld?.GetValue(attrs[type])?.ToString(); return str; } /// /// 设置类的CategoryOrderAttribute特性的值 /// /// /// public void SetCategoryOrderValue(object obj,CategoryOrderAttribute[] categoryOrders) { TypeDescriptor.AddAttributes(obj.GetType(),categoryOrders); } /// /// 使用反射设置嵌套属性的值 /// /// 对象 /// 第一层属性 /// 目标属性(是第一层属性的成员) /// 赋的值 private void NestPropeSetValue(object obj,string propename,string targetname,object val) { PropertyInfo[] propertyInfos = obj.GetType().GetProperties(); var prope = propertyInfos.Single(x => propename == x.PropertyType.Name); var value = prope.GetValue(obj,null); var pr = value.GetType().GetProperties().Single(x => x.Name == targetname); pr?.SetValue(value,val); } #endregion Dictionary EventName = new Dictionary { {"Name","名称" }, {"Text","文本" }, {"Content","按钮文本" }, {"Title","标题" }, {"Value","变量" }, {"BindingIsChecked","勾选状态" }, {"ClickExec","点击事件" }, {"ValueChangedExecute" , "值改变事件"}, {"TikcExecute" , "定时触发"}, {"CheckedExec" , "勾选事件"}, {"UnCheckedExec" , "取消勾选事件"}, {"EventReceiveNameList" , "接收消息集"}, { "TimeCount" , "定时间隔"}, { "InterfaceMode" , "接口类型"}, { "InterfaceParameters" , "接口参数"}, { "DataSouceInformation" , "连接信息"}, { "DeviceName" , "设备名称"}, { "DeviceValuleName" , "设备解析变量"}, {"FDataSouce" , "数据源"}, { "Code" , "代码过滤脚本"}, { "GenerateData" , "数据结果"}, }; } [CategoryOrder("基本属性",1), CategoryOrder("事件",2)]//分组在排序 public class PropertyGridCommand :ObservableObject { private string _名称; private string _文本; private string _标题; private string _文本1; private object _变量; private string _勾选状态; [Category("基本属性"), Description("Name"), Browsable(false), PropertyOrder(1)] public string 名称 { get { return _名称; } set { if (_名称 == value) return; _名称 = value; OnPropertyChanged("名称"); } } [Category("基本属性"), Description("Text"), Browsable(true), PropertyOrder(3)] public string 文本 { get { return _文本; } set { if (_文本 == value) return; _文本 = value; OnPropertyChanged("文本"); } } [Category("基本属性"), Description("Title"), Browsable(true), PropertyOrder(2)] public string 标题 { get { return _标题; } set { if (_标题 == value) return; _标题 = value; OnPropertyChanged("标题"); } } [Category("基本属性"), Description("Content"), Browsable(true), PropertyOrder(3)] public string 按钮文本 { get { return _文本1; } set { if (_文本1 == value) return; _文本1 = value; OnPropertyChanged("按钮文本"); } } [Category("基本属性"), Description("Value"), Browsable(true), PropertyOrder(4)] public object 变量 { get { return _变量; } set { if (_变量 == value) return; _变量 = value; OnPropertyChanged("变量"); } } [Category("基本属性"), Description("BindingIsChecked"), Browsable(false), PropertyOrder(5)] public string 勾选状态 { get { return _勾选状态; } set { if (_勾选状态 == value) return; _勾选状态 = value; OnPropertyChanged("勾选状态"); } } //[Category("基本属性"), Description("Content"), Browsable(true), PropertyOrder(2)] //public string 内容 { get; set; } //[Category("基本属性"), Description("Header"), Browsable(true), PropertyOrder(3)] //public string 标题 { get; set; } //[Category("基本属性"), Description("Text"), Browsable(true), PropertyOrder(4)] //public object 文本 { get; set; } //[Category("基本属性"), Description("NumberValue"), Browsable(true), PropertyOrder(5)] //public object 值 { get; set; } //[Category("基本属性"), Description("CurValue"), Browsable(true), PropertyOrder(5)] //public object 数值 { get; set; } //[Category("基本属性"), Description("StatusValue"), Browsable(true), PropertyOrder(6)] //public string 状态值 { get; set; } //[Category("基本属性"), Description("IsChecked"), Browsable(true), PropertyOrder(7)] //public string 勾选状态 { get; set; } //[Category("基本属性"), Description("Value"), Browsable(true), PropertyOrder(7)] //public string 数值1 { get; set; } //[Category("基本属性"), Description("MaxValue"), Browsable(true), PropertyOrder(8)] //public string 最大值 { get; set; } //[Category("基本属性"), Description("MinValue"), Browsable(true), PropertyOrder(9)] //public object 最小值 { get; set; } //[Category("基本属性"), Description("Maximum"), Browsable(true), PropertyOrder(8)] //public string 最大值1 { get; set; } //[Category("基本属性"), Description("Minimum"), Browsable(true), PropertyOrder(9)] //public object 最小值1 { get; set; } //[Category("基本属性"), Description("Interval"), Browsable(true), PropertyOrder(10)] //public object 间隔 { get; set; } //[Category("基本属性"), Description("左边距"), Browsable(true), PropertyOrder(11)] //public string 左边距 { get; set; } //[Category("基本属性"), Description("上边距"), Browsable(true), PropertyOrder(12)] //public string 上边距 { get; set; } //事件处理中心 private string _点击事件; private string _值改变事件; private string _定时触发; private string _勾选事件; private string _取消勾选事件; private ObservableCollection _接收消息集; [Category("事件-内部触发"), Description("ClickExec"), Browsable(true), PropertyOrder(1)] public string 点击事件 { get { return _点击事件; } set { if (_点击事件 == value) return; _点击事件 = value; OnPropertyChanged("点击事件"); } } [Category("事件-内部触发"), Description("ValueChangedExecute"), Browsable(true), PropertyOrder(2)] public string 值改变事件 { get { return _值改变事件; } set { if (_值改变事件 == value) return; _值改变事件 = value; OnPropertyChanged("值改变事件"); } } [Category("事件-内部触发"), Description("TikcExecute"), Browsable(true), PropertyOrder(3)] public string 定时触发 { get { return _定时触发; } set { if (_定时触发 == value) return; _定时触发 = value; OnPropertyChanged("定时触发"); } } [Category("事件-内部触发"), Description("CheckedExec"), Browsable(true), PropertyOrder(4)] public string 勾选事件 { get { return _勾选事件; } set { if (_勾选事件 == value) return; _勾选事件 = value; OnPropertyChanged("勾选事件"); } } [Category("事件-内部触发"), Description("UnCheckedExec"), Browsable(true), PropertyOrder(5)] public string 取消勾选事件 { get { return _取消勾选事件; } set { if (_取消勾选事件 == value) return; _取消勾选事件 = value; OnPropertyChanged("取消勾选事件"); } } [Category("外部-内部接收消息集"), Description("EventReceiveNameList"), Browsable(true), PropertyOrder(2)] public ObservableCollection 接收消息集 { get { return _接收消息集; } set { if (_接收消息集 == value) return; _接收消息集 = value; OnPropertyChanged("接收消息集"); } } private string _设备名称; [Category("*数据绑定模块*"), Description("DeviceName"), Browsable(true), PropertyOrder(2)] public string 设备名称 { get { return _设备名称; } set { if (_设备名称 == value) return; _设备名称 = value; OnPropertyChanged("设备名称"); } } private string _设备解析变量; [Category("*数据绑定模块*"), Description("DeviceValuleName"), Browsable(true), PropertyOrder(3)] public string 设备解析变量 { get { return _设备解析变量; } set { if (_设备解析变量 == value) return; _设备解析变量 = value; OnPropertyChanged("设备解析变量"); } } private InterfaceModeEnum _接口类型; [Category("*数据绑定模块*"), Description("InterfaceMode"), Browsable(true), PropertyOrder(4)] public InterfaceModeEnum 接口类型 { get { return _接口类型; } set { if (_接口类型 == value) return; _接口类型 = value; OnPropertyChanged("接口类型"); } } private string _接口参数; [Category("*数据绑定模块*"), Description("InterfaceParameters"), Browsable(true), PropertyOrder(5)] public string 接口参数 { get { return _接口参数; } set { if (_接口参数 == value) return; _接口参数 = value; OnPropertyChanged("接口参数"); } } private string _数据来源变量; [Category("*数据绑定模块*"), Description("DataSouceInformation"), Browsable(true), PropertyOrder(4)] public string 连接信息 { get { return _数据来源变量; } set { if (_数据来源变量 == value) return; _数据来源变量 = value; OnPropertyChanged("连接信息"); } } private string _数据源; [Category("*数据绑定模块*"), Description("FDataSouce"), Browsable(true), PropertyOrder(5)] public string 数据源 { get { return _数据源; } set { if (_数据源 == value) return; _数据源 = value; OnPropertyChanged("数据源"); } } private string _代码过滤脚本; [Category("*数据绑定模块*"), Description("Code"), Browsable(true), PropertyOrder(6)] public string 代码过滤脚本 { get { return _代码过滤脚本; } set { if (_代码过滤脚本 == value) return; _代码过滤脚本 = value; OnPropertyChanged("代码过滤脚本"); } } private string _数据结果; [Category("*数据绑定模块*"), Description("GenerateData"), Browsable(true), PropertyOrder(7)] public string 数据结果 { get { return _数据结果; } set { if (_数据结果 == value) return; _数据结果 = value; OnPropertyChanged("数据结果"); } } private int _定时调用; [Category("*数据绑定模块*"), Description("TimeCount"), Browsable(true), PropertyOrder(100)] public int 定时间隔 { get { return _定时调用; } set { if (_定时调用 == value) return; _定时调用 = value; OnPropertyChanged("定时间隔"); } } } }