|
- using BeDesignerSCADA.Common;
- using BeDesignerSCADA.View;
- using BeDesignerSCADA.ViewModel;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Forms;
- using System.Windows.Input;
- using System.Windows.Markup;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
-
- namespace BeDesignerSCADA.Controls
- {
- /// <summary>
- /// CanvasPanelNew.xaml 的交互逻辑
- /// </summary>
- public partial class CanvasPanelNew : System.Windows.Controls.UserControl
- {
- /// <summary>
- /// 界面Model
- /// </summary>
- public MainViewModelNew viewModel = new MainViewModelNew();
- /// <summary>
- /// 文件夹监视
- /// </summary>
- public FileSystemWatcher _watcher;
- public CanvasPanelNew(string _Path)
- {
- InitializeComponent();
- EditorHelper.Register<BindingExpression, BindingConvertor>();
- viewModel.LayoutsPath = _Path;
- this.DataContext = viewModel;
- viewModel.Loaded(cav, runCanvas);
-
- //控件加载
- Assembly assembly = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.SCADAControl.dll"); //Assembly.GetExecutingAssembly();
- List<Type> types = assembly.GetTypes().Where(t => t.GetInterface("IExecutable") != null)?.ToList();
- List<Type> typesView = new List<Type>();
- viewModel.ControlsNameValues.ToList().OrderBy(o => o.Value)?.ToList().ForEach(par =>
- {
- Type type= types?.Find(p => p.Name == par.Key);
- if (type != null)
- typesView.Add(type);
- });
- CtlList.ItemsSource = typesView;
-
- //读取文件
- CreateDir();
- FileRead(_Path);
-
- cav.SelectedItemAction += new Action<FrameworkElement>(o => {
- if (ReditSeleceTab.SelectedIndex!=1)
- ReditSeleceTab.SelectedIndex = 1;
- });
- }
-
- #region 位置调整
- /// <summary>
- /// 左对齐
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AglinLeftBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.AlignLeft();
- }
- /// <summary>
- /// 底部对齐
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AglinBottomBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.AlignBottom();
- }
- /// <summary>
- /// 顶部对齐
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AglinTopBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.AlignTop();
- }
- /// <summary>
- /// 右对齐
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AglinRightBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.AlignRight();
- }
- /// <summary>
- /// 居中
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AglinCenterBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.AlignCenter();
- }
- /// <summary>
- /// 垂直分布
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void VerticalLayoutBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.VertialLayout();
- }
- /// <summary>
- /// 水平分布
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void HorizontalLayoutBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.HorizontalLayout();
- }
- #endregion
-
- #region 变量管理器文件夹监听事件
- /// <summary>
- /// 创建文件夹,监视文件夹
- /// </summary>
- public void CreateDir()
- {
- try
- {
- //不存在则创建布局文件夹
- if (!Directory.Exists($"{System.AppDomain.CurrentDomain.BaseDirectory}Layouts"))
- {
- Directory.CreateDirectory($"{System.AppDomain.CurrentDomain.BaseDirectory}Layouts");
- }
- //不存在则创建变量管理器路径 AccessFile
- if (!Directory.Exists($"{System.AppDomain.CurrentDomain.BaseDirectory}AccessFile"))
- {
- Directory.CreateDirectory($"{System.AppDomain.CurrentDomain.BaseDirectory}AccessFile");
- }
- if (!Directory.Exists($"{System.AppDomain.CurrentDomain.BaseDirectory}AccessFile/JSON"))
- {
- Directory.CreateDirectory($"{System.AppDomain.CurrentDomain.BaseDirectory}AccessFile/JSON");
- }
-
- try
- {
-
- this._watcher = new FileSystemWatcher();
- _watcher.Path = $"{System.AppDomain.CurrentDomain.BaseDirectory}AccessFile/JSON";
- _watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.DirectoryName;
- _watcher.IncludeSubdirectories = true;
- _watcher.Created += new FileSystemEventHandler(FileWatcher_Created);
- _watcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
- _watcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
- _watcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
- Start();
- }
-
- catch (Exception ex)
- {
- Console.WriteLine("Error:" + ex.Message);
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 监视启动
- /// </summary>
- public void Start()
- {
- this._watcher.EnableRaisingEvents = true;
- Console.WriteLine("文件监控已经启动...");
- ReadFileVlaue();
- }
- /// <summary>
- /// 监视停止
- /// </summary>
- public void Stop()
- {
- this._watcher.EnableRaisingEvents = false;
- this._watcher.Dispose();
- this._watcher = null;
- }
- /// <summary>
- /// 文件夹新增
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void FileWatcher_Created(object sender, FileSystemEventArgs e)
- {
- Console.WriteLine("新增:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
- ReadFileVlaue();
- }
- /// <summary>
- /// 文件名变更-文件大小变化
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void FileWatcher_Changed(object sender, FileSystemEventArgs e)
- {
- Console.WriteLine("变更:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
- ReadFileVlaue();
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void FileWatcher_Deleted(object sender, FileSystemEventArgs e)
- {
- Console.WriteLine("删除:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
- ReadFileVlaue();
- }
- /// <summary>
- /// 文件名
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void FileWatcher_Renamed(object sender, RenamedEventArgs e)
- {
- Console.WriteLine("重命名: OldPath:{0} NewPath:{1} OldFileName{2} NewFileName:{3}", e.OldFullPath, e.FullPath, e.OldName, e.Name);
- ReadFileVlaue();
- }
- /// <summary>
- /// 读取文件变量
- /// </summary>
- public void ReadFileVlaue()
- {
- try
- {
- Directory.CreateDirectory(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AccessFile\\JSON"));
- viewModel.VariablePath = AppDomain.CurrentDomain.BaseDirectory + "AccessFile\\JSON\\CommunicationPar.json";
- DataBusModel.GetInstance().RefreshVariableManager(viewModel.VariablePath);
- }
- catch (Exception ex)
- {
-
- }
- }
-
- #endregion
-
- #region 外部调用事件
- /// <summary>
- /// 传入变量管理器地址
- /// </summary>
- /// <param name="path"></param>
- public void VariableManagerPath(string path)
- {
- try
- {
- viewModel.VariablePath = path;
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 保存文件流 布局文件
- /// </summary>
- public void FileSave()
- {
- viewModel.SaveAllPageHeaderPath(viewModel.LayoutsPath);
- }
- /// <summary>
- /// 读取文件,加载布局
- /// </summary>
- /// <param name="path"></param>
- public void FileRead(string path)
- {
- try
- {
- if (File.Exists(path))
- {
- viewModel.LoadAllPageHeaderPath(path);
- }
- }
- catch (Exception ex)
- {
- }
- }
- /// <summary>
- /// 运行程序
- /// </summary>
- public void Run()
- {
- try
- {
- cav.ClearSelection();
- runCanvas.Run(cav.Generator());
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 停止运行程序
- /// </summary>
- public void Destory()
- {
- try
- {
- runCanvas.Destory();
- }
- catch (Exception ex)
- {
-
- throw;
- }
- }
- #endregion
-
- #region 执行操作事件
- /// <summary>
- /// 查询输入框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
- {
- try
- {
- System.Windows.Controls.TextBox textBox = sender as System.Windows.Controls.TextBox;
- var info = viewModel.ControlsNameValues?.ToList().Find(par => par.Value.ToLower().StartsWith(textBox.Text.ToLower()));
- if (info.Value.Key != null)
- {
- foreach (var item in CtlList.Items)
- {
- if ((item as Type).Name== info.Value.Key)
- {
- CtlList.ScrollIntoView(item);
- CtlList.SelectedItem = item;
- }
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
- /// <summary>
- /// 运行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void RunBtn_Click(object sender, RoutedEventArgs e)
- {
- if (sender is System.Windows.Controls.Button btn)
- {
- if (btn.Tag.ToString() == "运行")
- {
- cav.ClearSelection();
- runCanvas.Run(cav.Generator());
- }
- else if (btn.Tag.ToString() == "停止")
- {
- runCanvas.Destory();
- }
- }
- }
- /// <summary>
- /// 模拟运行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- RunWindows runWindows = null;
- private void MNRunBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.ClearSelection();
- if (runWindows != null)
- {
- runWindows.Close();
- runWindows = null;
- }
- runWindows = new RunWindows();
- runWindows.LoadingData(JsonConvert.DeserializeObject<MenuModel>(JsonConvert.SerializeObject(viewModel.MenuModel)));
- runWindows.Show();
- }
- /// <summary>
- /// 加载
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void LoadBtn_Click(object sender, RoutedEventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "布局文件|*.lay";
- if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- cav.Load(ofd.FileName);
- }
- }
- /// <summary>
- /// 保存
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void SaveBtn_Click(object sender, RoutedEventArgs e)
- {
- SaveFileDialog sfd = new SaveFileDialog();
- sfd.Filter = "布局文件|*.lay";
-
- if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- string str = cav.Save();
- File.WriteAllText(sfd.FileName, str, Encoding.Unicode);
- }
- }
- /// <summary>
- /// 保存页面
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void SavePage_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- LeftSelectTab.SelectedIndex = 0;
- if (viewModel.MenuModel.SelectPageModels == null)
- viewModel.MenuModel.SelectPageModels = viewModel.MenuModel.pageModels[0];
- viewModel.MenuModel.SelectPageModels.visual?.Children.Clear();
- foreach (FrameworkElement element in cav.Children)
- {
- string xamlText = XamlWriter.Save(element);
- FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement;
- viewModel.MenuModel.SelectPageModels.visual.Children.Add(item);
- }
- viewModel.UpdatePageBase(viewModel.MenuModel.SelectPageModels);
- }
- catch (Exception ex)
- {
-
- }
-
- }
- /// <summary>
- /// 选中Tab改变事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- try
- {
- if (LeftSelectTab.SelectedIndex <= 1 && ReditSeleceTab != null)
- {
- ReditSeleceTab.SelectedIndex = LeftSelectTab.SelectedIndex;
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 页选中改变事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PageList_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- ReditSeleceTab.SelectedIndex = 0;
- }
- #endregion
-
- #region 左侧控件栏移动
- /// <summary>
- /// 移动到右侧
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CtlList_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
- {
- if (CtlList.SelectedItem != null && e.LeftButton == MouseButtonState.Pressed)
- {
- DragDrop.DoDragDrop(CtlList, CtlList.SelectedItem, System.Windows.DragDropEffects.Copy);
- codeEditor.Text = cav.Save();
- }
- }
- /// <summary>
- /// 显示代码
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void showCode_Click(object sender, RoutedEventArgs e)
- {
- codeEditor.Text = cav.Save();
- }
- /// <summary>
- /// 编辑
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.Edit(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 路径资源选择
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void LJToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.SelectPath(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 选择进程
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void XZToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.SelectValuePath(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 设置子控件模板
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ChildToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.ChildEdit(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 数据绑定
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BingToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
-
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.BingEdit(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 清除绑定
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ClearBingToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
-
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.ClearBingEdit(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- #endregion
-
-
- }
- }
|