using BeDesignerSCADA.Common; using BeDesignerSCADA.Controls; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Xml; namespace BeDesignerSCADA.View { /// /// ChildEditWindow.xaml 的交互逻辑 /// public partial class ChildEditWindow : Window { public ChildEditWindow() { InitializeComponent(); EditorHelper.Register(); Owner = Application.Current.MainWindow; Closing += (s, e) => { e.Cancel = true; Hide(); }; } public static ChildEditWindow Instance { get; } = new ChildEditWindow(); public static string ShowEdit(string xml) { try { // 获取所有控件的可用属性 Instance.main.Child = new MainCanvasPanel(""); if (!string.IsNullOrEmpty(xml)) { FrameworkElement element = XamlReader.Parse(xml) as FrameworkElement; if (element != null && element is Canvas) { (Instance.main.Child as MainCanvasPanel).LoadFrameworkElement(((element as Canvas).Children)); } } Instance.ShowDialog(); if (Instance.IsOk) { Canvas grid = new Canvas(); List children = new List(); foreach (FrameworkElement item in (Instance.main.Child as MainCanvasPanel).cav.Children) { children.Add(item); } double gd = children==null || children.Count<=0?60: children.Max((FrameworkElement x) => Canvas.GetTop(x) + x.ActualHeight); double wd = children == null || children.Count <= 0 ? 100 : children.Max((FrameworkElement x) => Canvas.GetLeft(x) + x.ActualWidth); grid.Width = wd; grid.Height = gd+5; (Instance.main.Child as MainCanvasPanel).GetChildren()?.ForEach(child => { grid.Children.Add(child); }); return FrameworkElementToXml(grid); } else { return xml; } } catch (Exception ex) { return xml; } } /// /// 流转Xml /// /// public static string FrameworkElementToXml(FrameworkElement framework) { StringBuilder outstr = new StringBuilder(); try { //this code need for right XML fomating XmlWriterSettings settings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }; var dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings)) { //this string need for turning on expression saving mode XamlWriterMode = XamlWriterMode.Expression }; XamlWriter.Save(framework, dsm); } catch (Exception ex) { } return outstr.ToString(); } public bool IsOk { get; set; } private void CancelBtn_Click(object sender, RoutedEventArgs e) { IsOk = false; Close(); } private void ConfirmBtn_Click(object sender, RoutedEventArgs e) { IsOk = true; Close(); } } }