|
- 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 变量
- /// <summary>
- /// 变量管理器地址
- /// </summary>
- public string VariablePath = string.Empty;
- /// <summary>
- /// 布局路径
- /// </summary>
- public string LayoutsPath = string.Empty;
- /// <summary>
- /// 是否正在运行状态
- /// </summary>
- 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");
- }
- }
- /// <summary>
- /// 画布是否显示
- /// </summary>
- private Visibility _CanvasPanelVisibility;
- public Visibility CanvasPanelVisibility
- {
- get
- {
- return _CanvasPanelVisibility;
- }
- set
- {
- if (_CanvasPanelVisibility == value)
- return;
- _CanvasPanelVisibility = value;
- OnPropertyChanged("CanvasPanelVisibility");
- }
- }
- /// <summary>
- /// 运行代码是否显示
- /// </summary>
- private Visibility _RunCanvasVisibility;
- public Visibility RunCanvasVisibility
- {
- get
- {
- return _RunCanvasVisibility;
- }
- set
- {
- if (_RunCanvasVisibility == value)
- return;
- _RunCanvasVisibility = value;
- OnPropertyChanged("RunCanvasVisibility");
- }
- }
- /// <summary>
- /// 当前代码Xaml
- /// </summary>
- private string _XamlCode;
- public string XamlCode
- {
- get
- {
- return _XamlCode;
- }
- set
- {
- if (_XamlCode == value)
- return;
- _XamlCode = value;
- OnPropertyChanged("XamlCode");
- }
- }
- /// <summary>
- /// 选中控件
- /// </summary>
- private FrameworkElement _CanSelectedItem;
- public FrameworkElement CanSelectedItem
- {
- get
- {
- return _CanSelectedItem;
- }
- set
- {
- if (_CanSelectedItem == value)
- return;
- _CanSelectedItem = value;
- OnPropertyChanged("CanSelectedItem");
- }
- }
- /// <summary>
- /// 菜单Model
- /// </summary>
- private MenuModel _MenuModel;
- public MenuModel MenuModel
- {
- get
- {
- return _MenuModel;
- }
- set
- {
- _MenuModel = value;
- OnPropertyChanged("MenuModel");
- }
- }
- /// <summary>
- /// 控件集合名称映射
- /// </summary>
- public Dictionary<string, string> ControlsNameValues = new Dictionary<string, string>() {
-
- {"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 命令
- /// <summary>
- /// 启动或者停止
- /// </summary>
- public RelayCommand RunUiCommand { get; set; }
- /// <summary>
- /// 编辑
- /// </summary>
- public RelayCommand EditCommand { get; set; }
- #endregion
-
- #region 常用函数
- /// <summary>
- /// 显示当前xaml代码
- /// </summary>
- public void ShowCode(TextEditor textEditor)
- {
- textEditor.Text = canvasPanel.Save();
- }
- /// <summary>
- /// 更新页面
- /// </summary>
- public void UpdatePageBase(PageModel _pageModel)
- {
- try
- {
- List<string> ChildrenStr = new List<string>();
- foreach (FrameworkElement element in _pageModel.visual.Children)
- {
- string xamlText = XamlWriter.Save(element);
- ChildrenStr.Add(xamlText);
- }
- MenuModel.SelectPageModels.ChildrenStr= ChildrenStr;
- }
- catch (Exception ex)
- {
-
- }
- }
- #endregion
-
- #region 脚本编辑数据
- /// <summary>
- /// 当前编辑界面
- /// </summary>
- public CanvasPanel canvasPanel;
- /// <summary>
- /// 当前运行界面
- /// </summary>
- public object runCanvas;
- /// <summary>
- /// 加载
- /// </summary>
- /// <param name="obj"></param>
- public void Loaded(object objCan, object objRun)
- {
- canvasPanel = objCan as CanvasPanel;
-
- runCanvas = objRun;// as RunCanvas;
-
- InitCommand();
- }
- /// <summary>
- /// 编辑
- /// </summary>
- /// <param name="obj"></param>
- 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;
- }
- /// <summary>
- /// 子控件编辑
- /// </summary>
- /// <param name="obj"></param>
- 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;
- }
- /// <summary>
- /// 绑定编辑
- /// </summary>
- /// <param name="obj"></param>
- 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);
- }
- }
- }
- /// <summary>
- /// 清除绑定
- /// </summary>
- /// <param name="obj"></param>
- 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);
- }
- }
- /// <summary>
- /// 图片选择路径
- /// </summary>
- /// <param name="obj"></param>
- 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);
- }
- }
- }
- }
- }
- /// <summary>
- /// 选择路径
- /// </summary>
- /// <param name="obj"></param>
- 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
- /// <summary>
- /// 设置首页
- /// </summary>
- public RelayCommand SetHomePageCommand { get; set; }
- /// <summary>
- /// 向上移动
- /// </summary>
- public RelayCommand SetTopPageCommand { get; set; }
- /// <summary>
- /// 向下移动
- /// </summary>
- public RelayCommand SetBottomPageCommand { get; set; }
- /// <summary>
- /// 增加页-该项下增加
- /// </summary>
- public RelayCommand<object> AddPageCommand { get; set; }
- /// <summary>
- /// 删除该页
- /// </summary>
- public RelayCommand<object> DeletePageCommand { get; set; }
- /// <summary>
- /// 清除当前页子集
- /// </summary>
- public RelayCommand<object> ClearPageCommand { get; set; }
- /// <summary>
- /// 选中该页
- /// </summary>
- public RelayCommand<object> SelectPageCommand { get; set; }
- /// <summary>
- /// 导出页面文件
- /// </summary>
- public RelayCommand SaveAllPageCommand { get; set; }
- /// <summary>
- /// 另存页面文件
- /// </summary>
- public RelayCommand LWSaveAllPageCommand { get; set; }
- /// <summary>
- /// 加载页面文件
- /// </summary>
- public RelayCommand LoadAllPageCommand { get; set; }
- /// <summary>
- /// 加载变量文件
- /// </summary>
- public RelayCommand LoadValuesCommand { get; set; }
- /// <summary>
- /// 初始化命令
- /// </summary>
- public void InitCommand()
- {
- SetHomePageCommand = new RelayCommand(new Action(SetHomePageHeader));
- SetTopPageCommand = new RelayCommand(new Action(SetTopPageHeader));
- SetBottomPageCommand = new RelayCommand(new Action(SetBottomPageHeader));
- AddPageCommand = new RelayCommand<object>(new Action<object?>(AddPageHeader));
- DeletePageCommand = new RelayCommand<object>(new Action<object?>(DeletePageHeader));
- ClearPageCommand = new RelayCommand<object>(new Action<object?>(ClearPageHeader));
- SelectPageCommand = new RelayCommand<object>(new Action<object?>(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<PageModel>();
- MenuModel.pageModels.Add(new PageModel { Name = "首页面", Sort = 0 });
- MenuModel.SelectPageModels = MenuModel.pageModels[0];
- GxSortTarget();
- }
- /// <summary>
- /// 刷新排序
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 设置首页
- /// </summary>
- 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)
- {
-
- }
- }
- /// <summary>
- /// 向上移动
- /// </summary>
- 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)
- {
-
- }
- }
- /// <summary>
- /// 向下移动
- /// </summary>
- 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)
- {
-
- }
- }
- /// <summary>
- /// 增加页-该项下增加
- /// </summary>
- 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)
- {
-
- }
- }
- /// <summary>
- /// 删除该页
- /// </summary>
- 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)
- {
-
- }
- }
- /// <summary>
- /// 清空该页
- /// </summary>
- /// <param name="obj"></param>
- 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)
- {
-
- }
- }
- /// <summary>
- /// 选中该页
- /// </summary>
- /// <param name="obj"></param>
- 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)
- {
-
- }
- }
- /// <summary>
- /// 保存页面文件
- /// </summary>
- 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)
- {
-
- }
- }
- /// <summary>
- /// 另存文件
- /// </summary>
- public void LWSaveAllPageHeader()
- {
- try
- {
- SaveFileDialog sfd = new SaveFileDialog();
- sfd.Filter = "页面布局|*.yf";
- if (sfd.ShowDialog() == true)
- {
- SaveAllPageHeaderPath(sfd.FileName);
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 根据路径保存菜单布局
- /// </summary>
- /// <param name="path"></param>
- 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();
- }
- }
- /// <summary>
- /// 加载页面文件
- /// </summary>
- public void LoadAllPageHeader()
- {
- try
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "页面布局|*.yf";
- if (ofd.ShowDialog() == true)
- {
- LoadAllPageHeaderPath(ofd.FileName);
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 根据路径加载菜单布局
- /// </summary>
- /// <param name="path"></param>
- public void LoadAllPageHeaderPath(string path)
- {
- try
- {
- MenuModel = BinaryFile.ReadBinary<MenuModel>(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)
- {
-
- }
- }
- /// <summary>
- /// 加载变量文件
- /// </summary>
- 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
- }
- }
|