using BeDesignerSCADA.Common; using BeDesignerSCADA.View; using BeDesignerSCADA.ViewModel; 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 { /// /// MainCanvasPanel.xaml 的交互逻辑 /// public partial class MainCanvasPanel : System.Windows.Controls.UserControl { /// /// 布局文件路径 /// public string Path = string.Empty; List controls = new List(); MainViewModelNew viewModel = new MainViewModelNew(); public MainCanvasPanel(string _Path) { InitializeComponent(); EditorHelper.Register(); Path = _Path; viewModel.LayoutsPath=Path; this.DataContext = viewModel; viewModel.Loaded(cav, runCanvas); //控件加载 Assembly assembly = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.SCADAControl.dll"); //Assembly.GetExecutingAssembly(); controls = assembly.GetTypes().Where(t => t.GetInterface("IExecutable") != null).OrderBy(o => o.Name)?.ToList(); //controls.Add(new Line().GetType()); CtlList.ItemsSource = controls; //读取文件 FileRead(_Path); } #region 位置调整 /// /// 左对齐 /// /// /// private void AglinLeftBtn_Click(object sender, RoutedEventArgs e) { cav.AlignLeft(); } /// /// 底部对齐 /// /// /// private void AglinBottomBtn_Click(object sender, RoutedEventArgs e) { cav.AlignBottom(); } /// /// 顶部对齐 /// /// /// private void AglinTopBtn_Click(object sender, RoutedEventArgs e) { cav.AlignTop(); } /// /// 右对齐 /// /// /// private void AglinRightBtn_Click(object sender, RoutedEventArgs e) { cav.AlignRight(); } /// /// 居中 /// /// /// private void AglinCenterBtn_Click(object sender, RoutedEventArgs e) { cav.AlignCenter(); } /// /// 垂直分布 /// /// /// private void VerticalLayoutBtn_Click(object sender, RoutedEventArgs e) { cav.VertialLayout(); } /// /// 水平分布 /// /// /// private void HorizontalLayoutBtn_Click(object sender, RoutedEventArgs e) { cav.HorizontalLayout(); } #endregion #region 其他事件操作 /// /// 传入变量管理器地址 /// /// private void LoadBtnValue_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "变量管理器文件|*.json"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { viewModel.VariablePath = ofd.FileName; bool isSucess= DataBusModel.GetInstance().RefreshVariableManager(ofd.FileName); System.Windows.MessageBox.Show(isSucess ? "加载成功!" : "加载失败!"); } } /// /// 运行 /// /// /// 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(); } } } /// /// 模拟运行 /// /// /// private void MNRunBtn_Click(object sender, RoutedEventArgs e) { cav.ClearSelection(); RunWindowsLao runWindows = new RunWindowsLao(); runWindows.LoadingData(cav.Generator()); runWindows.Show(); } /// /// 加载 /// /// /// 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); } /// /// 读取文件 /// /// public void FileRead(string path) { try { if (File.Exists(path)) { cav.Load(path); 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); } } catch (Exception ex) { } } /// /// 根据流加载数据 /// public void LoadFrameworkElement(UIElementCollection frameworks) { try { cav.Children.Clear(); foreach (FrameworkElement element in frameworks) { string xamlText = XamlWriter.Save(element); FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement; cav.Children.Add(item); } cav.SelectedItems?.Clear(); } catch (Exception ex) { throw; } } /// /// 获取所有控件 /// /// public List GetChildren() { List frameworks = new List(); foreach (FrameworkElement child in cav.Children) { string xamlText = XamlWriter.Save(child); FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement; frameworks.Add(item); } return frameworks; } /// /// 保存文件 /// public void FileSave() { SaveBtn_Click(null, null); } /// /// 传入变量管理器地址 /// /// public void VariableManagerPath(string path) { try { viewModel.VariablePath = path; } catch (Exception ex) { } } /// /// 运行程序 /// public void Run() { try { cav.ClearSelection(); runCanvas.Run(cav.Generator()); } catch (Exception ex) { } } /// /// 停止运行程序 /// public void Destory() { try { runCanvas.Destory(); } catch (Exception ex) { throw; } } /// /// 保存 /// /// /// private void SaveBtn_Click(object sender, RoutedEventArgs e) { if (File.Exists(Path)) { string str = cav.Save(); File.WriteAllText(Path, str, Encoding.Unicode); } else { 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); } } } /// /// 模拟消息发送 /// /// /// private void MNBtn_Click(object sender, RoutedEventArgs e) { } #endregion #region 左侧控件栏移动 /// /// 移动到右侧 /// /// /// 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(); } } /// /// 显示代码 /// /// /// private void showCode_Click(object sender, RoutedEventArgs e) { codeEditor.Text = cav.Save(); } /// /// 编辑 /// /// /// 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) { } } /// /// 路径资源选择 /// /// /// 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) { } } /// /// 选择进程 /// /// /// 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) { } } /// /// 设置子控件模板 /// /// /// 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) { } } /// /// 数据绑定 /// /// /// 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) { } } /// /// 清除绑定 /// /// /// 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 } }