使用运行时生成的控件保存控件窗口并重新加载为以前的状态
Posted
技术标签:
【中文标题】使用运行时生成的控件保存控件窗口并重新加载为以前的状态【英文标题】:Save window of controls with runtime geneated controls and reload with as previous state 【发布时间】:2012-10-17 07:10:42 【问题描述】:我用你的控制按钮创建了项目。 AddButton 创建运行时按钮控件并将其添加到 canvas1 中。当我右键单击运行时生成的控件时,会弹出新窗口。我想保存这个窗口并以与我离开时相同的状态重新加载它。我使用 xml 序列化,它会加载但单击该按钮不起作用。
这是我的代码:
private void AddButton_Click(object sender, RoutedEventArgs e)
Button b = new Button();
b.MinHeight = 23;
b.MinWidth = 73;
b.SetValue(Canvas.LeftProperty, e.GetPosition(this).X);
b.SetValue(Canvas.TopProperty, e.GetPosition(this).Y);
b.Content = i.ToString();
canvas1.Children.Add(b);
b.MouseRightButtonUp += new MouseButtonEventHandler(NewButton_Click);
private void NewButton_Click(object sender, EventArgs e)
Button btn = (Button)sender;
ContextMenu pMenu = new ContextMenu();
MenuItem item1 = new MenuItem();
item1.Header = "Properties";
pMenu.Items.Add(item1);
btn.ContextMenu = pMenu;
item1.Click += new RoutedEventHandler(item1_Click);
private void item1_Click(object sender, EventArgs e)
MenuItem btn = (MenuItem)sender;
ControlProp c = new ControlProp();
c.ShowDialog();
private void Save_Click(object sender, RoutedEventArgs e)
StringBuilder outstr = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.NewLineOnAttributes = true;
XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
dsm.XamlWriterMode = XamlWriterMode.Expression;
XamlWriter.Save(this.canvas1, dsm);
string savedControls = outstr.ToString();
//Show Dialog Box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".xaml"; // Default file extension
dlg.Filter = "Xaml documents (.xaml)|*.xaml"; // Filter files by extension
// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
// Save document
string filename = dlg.FileName;
File.WriteAllText(filename, savedControls);
private void OpenFile_Click(object sender, RoutedEventArgs e)
//Open FileDialog
string filename;
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".xaml"; // Default file extension
dlg.Filter = "Xaml documents (.xaml)|*.xaml"; // Filter files by extension
// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
// Save document
filename = dlg.FileName;
//Open Xaml
StreamReader sR = new StreamReader(filename);
string text = sR.ReadToEnd();
sR.Close();
StringReader stringReader = new StringReader(text);
XmlReader xmlReader = XmlReader.Create(stringReader);
Canvas wp = (Canvas)System.Windows.Markup.XamlReader.Load(xmlReader);
canvas1.Children.Clear(); // clear the existing children
foreach (FrameworkElement child in wp.Children) // and for each child in the WrapPanel we just loaded (wp)
canvas1.Children.Add(CloneFrameworkElement(child)); // clone the child and add it to our existing wrap panel
FrameworkElement CloneFrameworkElement(FrameworkElement originalElement)
string elementString = XamlWriter.Save(originalElement);
StringReader stringReader = new StringReader(elementString);
XmlReader xmlReader = XmlReader.Create(stringReader);
FrameworkElement clonedElement = (FrameworkElement)XamlReader.Load(xmlReader);
return clonedElement;
简而言之,我想保存用户在运行时创建的项目,然后像 mspaint 一样使用保存项目和打开项目工具重新打开它。
【问题讨论】:
【参考方案1】:您可以使用 XamlWriter (MSDN)
【讨论】:
以上是关于使用运行时生成的控件保存控件窗口并重新加载为以前的状态的主要内容,如果未能解决你的问题,请参考以下文章
款式修改窗口,开发调整过窗口格局保存功能,关了窗口重新打开还是按关闭前的格局.
vs写c#时,自动根据控件生成的代码不小心被删除了,窗口设计还在,如何重新自动生成?