【发布时间】:2018-01-30 02:24:54
【问题描述】:
我们已经在整个堆栈溢出和类似网站上搜索了适用于我们的应用程序的东西,但一切都让我们只成功了一半。
我们有一个应用程序允许用户将设备拖放到放置画布上。放下设备后,会创建它们的“路由器属性”,您可以更改它们的名称、地址、添加注释。
我们还让用户在设备之间连接线路。 (我们还将创建的路由器属性添加到可观察集合中)。
我们尝试过xmlserialization,它让我们保存了设备的物理端,但是在加载 xml 文件时,它不再有地址、注释等附加到任何保存的设备,并且不允许用于添加连接或转到其属性。
我意识到我们需要以某种方式序列化背后的代码,然后在反序列化时将其添加回每个设备,但我们似乎无法找到一种方法来序列化可观察的路由器属性集合。
对于允许我们保存画布、子项及其属性背后的代码的最简单方法,是否有人有任何建议?我附上图片以供参考,路由器属性类,如果需要,我很乐意包含任何代码。我们非常感谢任何帮助。
热烈的问候, 泰勒
例如
类
public class RouterProperties : INotifyPropertyChanged
{
private ArrayList incomingConnections = new ArrayList();
private ArrayList outgoingCnnections = new ArrayList();
private bool isLocked = true;
private bool isSelected = false;
private string deviceName = "Router";
private string hostName = "Host name";
private string routerIP = "192.168.0.1";
private string note = "Notes";
private string status = "Yellow";
private BitmapImage icon;
// getters and setters removed for brevity
public ArrayList IncomingConnections
...
public ArrayList OutgoingCnnections
...
public bool IsLocked
...
public bool IsSelected
...
public string DeviceName
...
public string HostName
...
public string RouterIP
...
public string Note
...
public string Status
...
public BitmapImage Icon
...
主窗口类
public ObservableCollection<RouterProperties> devices = new ObservableCollection<RouterProperties>();
编辑保存xaml的代码
// De-Serialize XML to UIElement using a given filename.
public static UIElement DeSerializeXAML(string filename)
{
// Load XAML from file. Use 'using' so objects are disposed of properly.
using (System.IO.FileStream fs = System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
return System.Windows.Markup.XamlReader.Load(fs) as UIElement;
}
}
// Serializes any UIElement object to XAML using a given filename.
public static void SerializeToXAML(UIElement element, string filename)
{
// Use XamlWriter object to serialize element
string strXAML = System.Windows.Markup.XamlWriter.Save(element);
// Write XAML to file. Use 'using' so objects are disposed of properly.
using (System.IO.FileStream fs = System.IO.File.Create(filename))
{
using (System.IO.StreamWriter streamwriter = new System.IO.StreamWriter(fs))
{
streamwriter.Write(strXAML);
}
}
}
private void menuSave_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "UIElement File"; // Default file name
dlg.DefaultExt = ".xaml"; // Default file extension
dlg.Filter = "Xaml File (.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;
SerializeToXAML(canvasMain, filename);
}
}
private void menuLoad_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".xaml"; // Default file extension
dlg.Filter = "Xaml File (.xaml)|*.xaml"; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
string filename = dlg.FileName;
Canvas canvas = DeSerializeXAML(filename) as Canvas;
// Add all child elements (lines, rectangles etc) to canvas
while (canvas.Children.Count > 0)
{
UIElement obj = canvas.Children[0]; // Get next child
canvas.Children.Remove(obj); // Have to disconnect it from result before we can add it
canvasMain.Children.Add(obj); // Add to canvas
}
}
}
【问题讨论】:
-
当你说序列化后面的代码时,你到底是什么意思?那里保留了哪些属性和状态?
-
每个设备“属性”只是c#中存在的变量,而不是xaml中的变量,直到它们被调用到第二张图片的属性窗口中的文本框中。序列化仅保存物理 xaml 属性,因此当我们加载保存的 xml 文件时,设备存在于画布上的确切位置,但没有任何已保存的 c# 变量已附加到它们。设备名称、主机名、注释等属性。路由器属性中的字符串。
-
你能显示保存xmal的代码吗
-
感谢您查看此内容。添加了代码以在主帖子中的编辑下保存 xaml
标签: c# wpf serialization dynamicobject