using BeDesignerSCADA.Common; using BeDesignerSCADA.Controls; using BeDesignerSCADA.View; using BPASmartClient.Compiler; using BPASmartClient.SCADAControl.Converters; using ICSharpCode.AvalonEdit; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using Microsoft.Win32; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows; using System.Windows.Data; using System.Windows.Markup; namespace BeDesignerSCADA.ViewModel { public class MainViewModelNew : ObservableObject { public MainViewModelNew() { IsRunning = false; //启动 或者 停止 RunUiCommand = new RelayCommand(() => { IsRunning = !IsRunning; }); } #region 变量 /// /// 变量管理器地址 /// public string VariablePath = string.Empty; /// /// 布局路径 /// public string LayoutsPath = string.Empty; /// /// 是否正在运行状态 /// private bool _IsRunning = false; public bool IsRunning { get { return _IsRunning; } set { _IsRunning = value; if (value) { RunCanvasVisibility = Visibility.Visible; CanvasPanelVisibility = Visibility.Collapsed; } else { RunCanvasVisibility = Visibility.Collapsed; 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; OnPropertyChanged("CanSelectedItem"); } } /// /// 菜单Model /// private MenuModel _MenuModel; public MenuModel MenuModel { get { return _MenuModel; } set { _MenuModel = value; OnPropertyChanged("MenuModel"); } } /// /// 控件集合名称映射 /// public Dictionary ControlsNameValues = new Dictionary() { {"TheButton","按钮" }, {"TheTimer","计时器" }, {"TheCheckBox","勾选框" }, {"DigitalNumber","液晶数字" }, {"TheComboBox","下拉框" }, {"TheImage","图片" }, {"TheTextBlock","文本块" }, {"ArcGauge","仪表盘" }, {"StatusLight","状态灯" }, {"TheToggleButton","开关按钮" }, {"TheGroupBox","分组" }, {"TheTextBox","文本框" }, {"NumberBox","数值框" }, {"GraphArrow","箭头" }, {"GraphStar","五角星" }, {"TheSlider","滑块" }, {"TheRadioButton","单选按钮" }, {"SwitchButton","开关" }, {"KnobButton","旋钮" }, {"Silos","物料仓" }, {"NewConveyorBelt","滚动线" }, {"TheDataGrid","表格" }, {"TheListBox","列表控件" }, {"TheRedis","Redis控件" }, {"WaveProgressBar","进度条波浪" }, {"TheProgressBar","进度条正常" }, {"TheRedProgressBar","进度条圆形红" }, {"TheBlueProgressBar","进度条圆形蓝" }, {"TheGreenProgressBar","进度条圆形绿" }, {"TheAPI","API接口" }, {"TheMQTT","MQTT" }, {"TheTabControl","选项卡控件" }, {"TheCylinder","气缸控件" }, {"TheRectangle","矩形" }, {"TheEllipse","圆形" }, {"TheWuLiaoControl","物料集合控件" }, {"FYFTheListBox","列表控件-拖拽" }, {"ThePopMessage","消息弹窗" }, {"TheMessage","消息提示" }, }; #endregion #region 命令 /// /// 启动或者停止 /// public RelayCommand RunUiCommand { get; set; } /// /// 编辑 /// public RelayCommand EditCommand { get; set; } #endregion #region 常用函数 /// /// 显示当前xaml代码 /// public void ShowCode(TextEditor textEditor) { textEditor.Text = canvasPanel.Save(); } /// /// 更新页面 /// public void UpdatePageBase(PageModel _pageModel) { try { List ChildrenStr = new List(); foreach (FrameworkElement element in _pageModel.visual.Children) { string xamlText = XamlWriter.Save(element); ChildrenStr.Add(xamlText); } MenuModel.SelectPageModels.ChildrenStr= ChildrenStr; } catch (Exception ex) { } } #endregion #region 脚本编辑数据 /// /// 当前编辑界面 /// public CanvasPanel canvasPanel; /// /// 当前运行界面 /// public object runCanvas; /// /// 加载 /// /// public void Loaded(object objCan, object objRun) { canvasPanel = objCan as CanvasPanel; runCanvas = objRun;// as RunCanvas; InitCommand(); } /// /// 编辑 /// /// 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; } /// /// 子控件编辑 /// /// public void ChildEdit(object obj) { string xml = ChildEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString()); ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = xml; } /// /// 绑定编辑 /// /// public void BingEdit(object obj) { //父控件 Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid _property = (Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid)((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).ParentElement; //选中控件 string controlname = _property.SelectedObjectName;//名称 Type type = _property.SelectedObjectType;//类型 object selectpro = _property.SelectedProperty; object selectControl = _property.SelectedObject;//对象 string selectProName = _property.SelectedProperty.ToString(); //选中属性 DependencyProperty dependencyProperty = PropertyHelper.GetDependencyProperties(selectControl)?.ToList().Find(x => x.Name == selectProName); if (dependencyProperty != null) { string _binding = PropertyBindingWindow.ShowEdit(dependencyProperty, (selectControl as FrameworkElement), VariablePath); if (!string.IsNullOrEmpty(_binding)) { Binding bindingText = new Binding() { Mode = BindingMode.TwoWay, Converter = new JTokenToBoolConverter(), RelativeSource = new RelativeSource { Mode = RelativeSourceMode.Self }, Path = new PropertyPath(_binding) }; (selectControl as FrameworkElement).SetBinding(dependencyProperty, bindingText); } else { BindingOperations.ClearBinding((selectControl as FrameworkElement), dependencyProperty); } } } /// /// 清除绑定 /// /// public void ClearBingEdit(object obj) { //父控件 Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid _property = (Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid)((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).ParentElement; //选中控件 string controlname = _property.SelectedObjectName;//名称 Type type = _property.SelectedObjectType;//类型 object selectpro = _property.SelectedProperty; object selectControl = _property.SelectedObject;//对象 string selectProName = _property.SelectedProperty.ToString(); //选中属性 DependencyProperty dependencyProperty = PropertyHelper.GetDependencyProperties(selectControl)?.ToList().Find(x => x.Name == selectProName); if (dependencyProperty != null) { BindingOperations.ClearBinding((selectControl as FrameworkElement), dependencyProperty); } } /// /// 图片选择路径 /// /// public void SelectPath(object obj) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp"; if (ofd.ShowDialog() == true) { if (!string.IsNullOrEmpty(LayoutsPath)) { string qz = Path.GetDirectoryName(LayoutsPath); if (!string.IsNullOrEmpty(qz) && qz.Contains("Layouts")) { string path = ofd.FileName; string path2 = $"{qz.Replace("Layouts", "Images")}/{ofd.SafeFileName}"; FileInfo fi1 = new FileInfo(path); FileInfo fi2 = new FileInfo(path2); try { if (!Directory.Exists(qz.Replace("Layouts", "Images"))) // 返回bool类型,存在返回true,不存在返回false { Directory.CreateDirectory(qz.Replace("Layouts", "Images")); //不存在则创建路径 } if (File.Exists(path2)) { fi2.Delete(); } fi1.CopyTo(path2); ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = System.IO.Path.GetFileName(ofd.FileName); } catch (IOException ioex) { Console.WriteLine(ioex.Message); } } } } } /// /// 选择路径 /// /// public void SelectValuePath(object obj) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "请选择|*.*"; if (ofd.ShowDialog() == true) { ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = ofd.SafeFileName; } } #endregion #region 命令事件,页面编辑 Command /// /// 设置首页 /// public RelayCommand SetHomePageCommand { get; set; } /// /// 向上移动 /// public RelayCommand SetTopPageCommand { get; set; } /// /// 向下移动 /// public RelayCommand SetBottomPageCommand { get; set; } /// /// 增加页-该项下增加 /// public RelayCommand AddPageCommand { get; set; } /// /// 删除该页 /// public RelayCommand DeletePageCommand { get; set; } /// /// 清除当前页子集 /// public RelayCommand ClearPageCommand { get; set; } /// /// 选中该页 /// public RelayCommand SelectPageCommand { get; set; } /// /// 导出页面文件 /// public RelayCommand SaveAllPageCommand { get; set; } /// /// 另存页面文件 /// public RelayCommand LWSaveAllPageCommand { get; set; } /// /// 加载页面文件 /// public RelayCommand LoadAllPageCommand { get; set; } /// /// 加载变量文件 /// public RelayCommand LoadValuesCommand { get; set; } /// /// 初始化命令 /// public void InitCommand() { SetHomePageCommand = new RelayCommand(new Action(SetHomePageHeader)); SetTopPageCommand = new RelayCommand(new Action(SetTopPageHeader)); SetBottomPageCommand = new RelayCommand(new Action(SetBottomPageHeader)); AddPageCommand = new RelayCommand(new Action(AddPageHeader)); DeletePageCommand = new RelayCommand(new Action(DeletePageHeader)); ClearPageCommand = new RelayCommand(new Action(ClearPageHeader)); SelectPageCommand = new RelayCommand(new Action(SelectPageHeader)); SaveAllPageCommand = new RelayCommand(new Action(SaveAllPageHeader)); LWSaveAllPageCommand=new RelayCommand(new Action(LWSaveAllPageHeader)); LoadAllPageCommand = new RelayCommand(new Action(LoadAllPageHeader)); LoadValuesCommand = new RelayCommand(new Action(LoadValuesHeader)); MenuModel = new MenuModel(); MenuModel.pageModels = new ObservableCollection(); MenuModel.pageModels.Add(new PageModel { Name = "首页面", Sort = 0 }); MenuModel.SelectPageModels = MenuModel.pageModels[0]; GxSortTarget(); } /// /// 刷新排序 /// public void GxSortTarget() { for (int i = 0; i < MenuModel.pageModels.Count; i++) { MenuModel.pageModels[i].Sort = i; if(i==0) MenuModel.pageModels[i].IsHome = true; else MenuModel.pageModels[i].IsHome = false; } } /// /// 设置首页 /// public void SetHomePageHeader() { try { if (MenuModel.SelectPageModels != null) { PageModel mode = MenuModel.SelectPageModels; MenuModel.pageModels.Remove(mode); MenuModel.pageModels.Insert(0,mode); GxSortTarget(); } } catch (Exception ex) { } } /// /// 向上移动 /// public void SetTopPageHeader() { try { if (MenuModel.SelectPageModels != null) { PageModel mode = MenuModel.SelectPageModels; if (mode.Sort > 0) { MenuModel.pageModels.Remove(mode); MenuModel.pageModels.Insert(mode.Sort - 1, mode); GxSortTarget(); } } } catch (Exception ex) { } } /// /// 向下移动 /// public void SetBottomPageHeader() { try { if (MenuModel.SelectPageModels != null) { PageModel mode = MenuModel.SelectPageModels; if (mode.Sort > 0 && mode.Sort+1< MenuModel.pageModels.Count) { MenuModel.pageModels.Remove(mode); MenuModel.pageModels.Insert(mode.Sort + 1, mode); GxSortTarget(); } } } catch (Exception ex) { } } /// /// 增加页-该项下增加 /// public void AddPageHeader(object obj) { try { if (obj != null && obj is PageModel mode) { if (mode.Sort >= 0 && mode.Sort + 1 < MenuModel.pageModels.Count) { MenuModel.pageModels.Insert(mode.Sort + 1,new PageModel { Name = "页面"+ MenuModel.pageModels.Count, Sort = MenuModel.pageModels.Count }); GxSortTarget(); } else if (mode.Sort + 1 == MenuModel.pageModels.Count) { MenuModel.pageModels.Add(new PageModel { Name = "页面" + MenuModel.pageModels.Count, Sort = MenuModel.pageModels.Count }); GxSortTarget(); } } } catch (Exception ex) { } } /// /// 删除该页 /// public void DeletePageHeader(object obj) { try { if (obj != null && obj is PageModel page && MenuModel.pageModels.Count > 1) { if (new MyMessageBox(MessageBoxStyle.question, $"当前选中页面名称:{page.Name},确认是否删除!").ShowDialog() == true) { if (MenuModel.SelectPageModels == page) { MenuModel.pageModels.Remove(page); GxSortTarget(); SelectPageHeader(MenuModel.pageModels[0]); } else { MenuModel.pageModels.Remove(page); GxSortTarget(); } } } else if (obj != null)//最后一页 { if (new MyMessageBox(MessageBoxStyle.question, "至少保留一页,是否清空该页!").ShowDialog() == true) { (obj as PageModel).visual.Children.Clear(); (obj as PageModel).ChildrenStr.Clear(); SelectPageHeader(MenuModel.pageModels[0]); } } } catch (Exception ex) { } } /// /// 清空该页 /// /// public void ClearPageHeader(object obj) { try { if (obj != null && obj is PageModel page) { if (new MyMessageBox(MessageBoxStyle.question, $"页面名称:{page.Name},是否清空该页!").ShowDialog() == true) { (obj as PageModel).visual.Children.Clear(); (obj as PageModel).ChildrenStr.Clear(); if (MenuModel.SelectPageModels == page) SelectPageHeader(obj); } } } catch (Exception ex) { } } /// /// 选中该页 /// /// public void SelectPageHeader(object obj) { try { MenuModel.SelectPageModels = obj as PageModel; try { canvasPanel.Children.Clear(); foreach (FrameworkElement element in (MenuModel.SelectPageModels?.visual as CanvasPanel).Children) { string xamlText = XamlWriter.Save(element); FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement; canvasPanel.Children.Add(item); } canvasPanel.SelectedItems?.Clear(); } catch (Exception ex) { } } catch (Exception ex) { } } /// /// 保存页面文件 /// public void SaveAllPageHeader() { try { string _path = $"{System.AppDomain.CurrentDomain.BaseDirectory}Layouts\\可视化界面菜单布局.yf"; if (!Directory.Exists($"{System.AppDomain.CurrentDomain.BaseDirectory}Layouts")) // 返回bool类型,存在返回true,不存在返回false { Directory.CreateDirectory($"{System.AppDomain.CurrentDomain.BaseDirectory}Layouts"); //不存在则创建路径 } if (File.Exists(_path)) // 返回bool类型,存在返回true,不存在返回false { File.Delete(_path); } SaveAllPageHeaderPath(_path); } catch (Exception ex) { } } /// /// 另存文件 /// public void LWSaveAllPageHeader() { try { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "页面布局|*.yf"; if (sfd.ShowDialog() == true) { SaveAllPageHeaderPath(sfd.FileName); } } catch (Exception ex) { } } /// /// 根据路径保存菜单布局 /// /// public void SaveAllPageHeaderPath(string path) { try { BinaryFile.SaveBinary(MenuModel, path); new MyMessageBox(MessageBoxStyle.info, $"保存成功!").ShowDialog(); } catch (Exception ex) { new MyMessageBox(MessageBoxStyle.error, "保存失败!原因:" + ex.Message).ShowDialog(); } } /// /// 加载页面文件 /// public void LoadAllPageHeader() { try { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "页面布局|*.yf"; if (ofd.ShowDialog() == true) { LoadAllPageHeaderPath(ofd.FileName); } } catch (Exception ex) { } } /// /// 根据路径加载菜单布局 /// /// public void LoadAllPageHeaderPath(string path) { try { MenuModel = BinaryFile.ReadBinary(path) as MenuModel; MenuModel?.pageModels?.ToList().ForEach(par => { par.visual = new CanvasPanel(); par?.ChildrenStr?.ForEach(k => { FrameworkElement item = XamlReader.Parse(k) as FrameworkElement; par.visual.Children.Add(item); }); }); if (MenuModel.pageModels != null && MenuModel.pageModels.Count > 0) { SelectPageHeader(MenuModel.pageModels?.ToList()?[0]); } } catch (Exception ex) { } } /// /// 加载变量文件 /// public void LoadValuesHeader() { try { string path= AppDomain.CurrentDomain.BaseDirectory + "BPASmart.VariableManager.exe"; if (File.Exists(path)) { Process.Start(path); } } catch (Exception ex) { } //OpenFileDialog ofd = new OpenFileDialog(); //ofd.Filter = "变量管理器文件|*.json"; //if (ofd.ShowDialog() == true) //{ // VariablePath = ofd.FileName; // bool isSucess = DataBusModel.GetInstance().RefreshVariableManager(ofd.FileName); // System.Windows.MessageBox.Show(isSucess ? "加载成功!" : "加载失败!"); //} } #endregion } }