I am trying to save the WPF code that i have inside a Canvas with serialization.
What I want to do is that i can add elements in my C# program and then when I press save I want to save the content of my canvas to a file.
I am using the SaveFileDialog to first open a Dialog were i can save the file.
But when I am trying to save it I have problems.
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Name_" + random_save_name;
dlg.DefaultExt = ".text";
dlg.Filter = "Text documents (.txt)|*.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
SerializeToXML(this.parentWindow, this.parentWindow.canvas, 96, dlg.FileName);
}
canvas is the name of my canvas, and the SerializeToXML locks like this.
public static void SerializeToXML(MainWindow window, Canvas canvas, int dpi, string filename)
{
XmlSerializer serializer = new XmlSerializer(typeof(Canvas));
TextWriter textWriter = new StreamWriter(filename);
serializer.Serialize(textWriter, canvas);
textWriter.Close();
}
I can run the program but i get an Exception. I am probably doing this wrong but i have had a hard time finding a solution to this.
Eddt:
“There was an error reflecting type ‘System.Windows.Controls.Canvas’.” and “Exception Intercepted: SerialixeToXML, too An exception was intercepted and the call stack unwound to the point before the call from user code where the exception occured. “Unwind the call stack on unhandled exceptions” is selected in the debugger options” i am not the best reading from the debugger in VS, that is what the live Event says
Edit 2: did find this: http://blogs.msdn.com/b/ashish/archive/2008/01/15/dynamically-producing-xaml-files-using-xamlwriter-save-method.aspx and now i can save it to an txt file.
What i did was:
This way i can save the canvas.