|
- using BeDesignerSCADA.ViewModel;
- using BPASmart.Model;
- using BPASmartClient.Compiler;
- using BPASmartClient.DATABUS;
- using BPASmartClient.MessageName;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Reflection.Emit;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Forms;
- using System.Windows.Input;
- using System.Windows.Markup;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
- namespace BeDesignerSCADA
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
-
- MainViewModel viewModel=new MainViewModel();
- public MainWindow()
- {
- InitializeComponent();
- this.DataContext = viewModel;
- viewModel.Loaded(cav, runCanvas);
- //控件加载
- Assembly assembly = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.SCADAControl.dll"); //Assembly.GetExecutingAssembly();
- var controls = assembly.GetTypes().Where(t => t.GetInterface("IExecutable") != null).OrderBy(o=>o.Name);
- CtlList.ItemsSource = controls;
- this.GotFocus += MainWindow_GotFocus;
- this.LostFocus += MainWindow_LostFocus;
- }
-
- private void MainWindow_LostFocus(object sender, RoutedEventArgs e)
- {
- System.Diagnostics.Trace.WriteLine("失去焦点:" + e.OriginalSource.ToString());
- // Console.WriteLine(e.OriginalSource.ToString());
- }
-
- private void MainWindow_GotFocus(object sender, RoutedEventArgs e)
- {
- System.Diagnostics.Trace.WriteLine("获得焦点:"+e.OriginalSource.ToString());
-
- }
-
- #region 加载数据中心与事件中心
- /// <summary>
- /// 加载数据中心
- /// </summary>
- public void Loading()
- {
- //try
- //{
- // Assembly assembly1 = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.MessageName.dll");
- // Type type = assembly1.GetType("BPASmartClient.MessageName.MessageName");
- // var control = Activator.CreateInstance(type);
- // var fildes = control.GetType().GetFields();
- // foreach (var fi in fildes)
- // {
- // EventModel eventModel = new EventModel();
- // eventModel.EventValue = fi.GetValue(control)?.ToString();
- // eventModel.EventName = fi.Name;
- // foreach (var item in fi.CustomAttributes)
- // {
- // if (item.AttributeType.Name == "CategoryAttribute")
- // {
- // eventModel.Category = item.ConstructorArguments.Count() > 0 ? item.ConstructorArguments[0].Value?.ToString() : "";
- // }
- // else if (item.AttributeType.Name == "DescriptionAttribute")
- // {
- // eventModel.Description = item.ConstructorArguments.Count() > 0 ? item.ConstructorArguments[0].Value?.ToString() : "";
- // }
- // else if (item.AttributeType.Name == "BrowsableAttribute")
- // {
- // eventModel.Browsable = item.ConstructorArguments.Count() > 0 ? (bool)item.ConstructorArguments[0].Value : true;
- // }
- // }
- // if (!string.IsNullOrEmpty(eventModel.Category))
- // {
- // if (Class_DataBus.GetInstance().EventData.ContainsKey(eventModel.Category))
- // Class_DataBus.GetInstance().EventData[eventModel.Category].Add(eventModel);
- // else
- // {
- // Class_DataBus.GetInstance().EventData[eventModel.Category] = new List<EventModel> { eventModel };
- // }
- // }
- // }
-
-
- // type = assembly1.GetType("BPASmartClient.MessageName.DataName");
- // control = Activator.CreateInstance(type);
- // fildes = control.GetType().GetFields();
- // foreach (var fi in fildes)
- // {
- // ALLModel aLLModel = new ALLModel();
- // aLLModel.DataValue = fi.GetValue(control)?.ToString();
- // aLLModel.DataName = fi.Name;
- // foreach (var item in fi.CustomAttributes)
- // {
- // if (item.AttributeType.Name == "CategoryAttribute")
- // {
- // aLLModel.Category = item.ConstructorArguments.Count() > 0 ? item.ConstructorArguments[0].Value?.ToString() : "";
- // }
- // else if (item.AttributeType.Name == "DescriptionAttribute")
- // {
- // aLLModel.Description = item.ConstructorArguments.Count() > 0 ? item.ConstructorArguments[0].Value?.ToString() : "";
- // }
- // else if (item.AttributeType.Name == "BrowsableAttribute")
- // {
- // aLLModel.Browsable = item.ConstructorArguments.Count() > 0 ? (bool)item.ConstructorArguments[0].Value : true;
- // }
- // }
- // if (!string.IsNullOrEmpty(aLLModel.Category))
- // {
- // if (Class_DataBus.GetInstance().ALLData.ContainsKey(aLLModel.Category))
- // Class_DataBus.GetInstance().ALLData[aLLModel.Category].Add(aLLModel);
- // else
- // {
- // Class_DataBus.GetInstance().ALLData[aLLModel.Category] = new List<ALLModel> { aLLModel };
- // }
- // }
-
- // }
-
-
- //}
- //catch (Exception ex)
- //{
- // System.Windows.MessageBox.Show(ex.Message);
- //}
- }
- /// <summary>
- /// 加载本地文件
- /// </summary>
- public void LoadFile()
- {
- //加载配置数据
- FJson<CommunicationPar>.Read();
- viewModel.DevNameList = new System.Collections.ObjectModel.ObservableCollection<string>();
- viewModel.DevValueList = new System.Collections.ObjectModel.ObservableCollection<string>();
- FJson<CommunicationPar>.Data.CommunicationDevices?.ToList().ForEach(data =>
- {
- viewModel.DevNameList.Add(data.DeviceName);
- data.VarTableModels?.ToList().ForEach((data) => {
-
- if(!viewModel.DevValueList.Contains(data.VarName) && !string.IsNullOrEmpty(data.VarName))
- viewModel.DevValueList.Add(data.VarName);
- });
- });
- //CommunicationModel model = new CommunicationModel();
- //Type typeData = model.GetType();
- //PropertyInfo[] properties = typeData.GetProperties();
- //foreach (PropertyInfo property in properties)
- //{
- // viewModel.DevValueList.Add(property.Name);
-
- //}
- //ICommunicationDevice DeviceType;
- //DeviceType = FJson<CommunicationPar>.Data.CommunicationDevices.ElementAt(0).CommDevice;
- //VariableInfo variable = new VariableInfo(DeviceType);
- //PropertyInfo[] prope = variable.GetType().GetProperties();
- //foreach (var item in prope)
- //{
- // viewModel.DevValueList.Add(item.Name);
- //}
- }
- #endregion
-
- #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>
- /// <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>
- 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);
- }
-
- DoubleAnimation da = new DoubleAnimation(-200, 0, new Duration(TimeSpan.FromMilliseconds(250)));
- da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
- CanvasTranslate.BeginAnimation(TranslateTransform.XProperty, da);
-
- DoubleAnimation daop = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(250)));
- daop.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
- cav.BeginAnimation(OpacityProperty, daop);
- }
- /// <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 MNBtn_Click(object sender, RoutedEventArgs e)
- {
-
- }
- #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 ComboBoxValue_TextChanged(object sender,TextChangedEventArgs e)
- {
- try
- {
- if (sender is System.Windows.Controls.ComboBox)
- {
- System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (toggle.Tag != null && !string.IsNullOrEmpty(toggle.Text))
- propertyGridCommand.Value = "{" + $"Binding {toggle.Tag}.{toggle.Text}" + "}";
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 变量下拉框打开事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void valuebox_DropDownOpened(object sender,EventArgs e)
- {
- try
- {
- viewModel.DevValueList = new System.Collections.ObjectModel.ObservableCollection<string>();
- if (sender is System.Windows.Controls.ComboBox)
- {
- System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
- if (toggle.Tag == null) return;
- if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(toggle.Tag.ToString()))
- {
- Class_DataBus.GetInstance().Dic_DeviceData[toggle.Tag.ToString()].Keys?.ToList().ForEach(key => { viewModel.DevValueList.Add(key); });
- }
- }
- }
- catch (Exception ex)
- {
-
- }
-
- }
- /// <summary>
- /// 设备名称选择
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ComboBoxName_TextChanged(object sender,TextChangedEventArgs e)
- {
- try
- {
- if (sender is System.Windows.Controls.ComboBox)
- {
- System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if(toggle.Tag!=null && !string.IsNullOrEmpty(toggle.Text))
- propertyGridCommand.Value = "{" + $"Binding {toggle.Text}.{toggle.Tag}" + "}";
- else if(!string.IsNullOrEmpty(toggle.Text))
- propertyGridCommand.Value = "{" + $"Binding {toggle.Text}." + "}";
-
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 设备名称下拉框打开事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void namebox_DropDownOpened(object sender,EventArgs e)
- {
- viewModel.DevNameList = new System.Collections.ObjectModel.ObservableCollection<string>();
- Class_DataBus.GetInstance().Dic_DeviceData.Keys?.ToList().ForEach(key => { viewModel.DevNameList.Add(key); });
- }
- #endregion
- }
- }
|