Click here to Skip to main content
14,578,799 members

Serialize DependencyObject: It's easy !

Rate this:
5.00 (1 vote)
Please Sign up or sign in to vote.
5.00 (1 vote)
25 Feb 2010Ms-PL
An easy way to perform serialization of these objects

You often read on the web that the DependencyObjects are not marked as serializable and that this is a major drawback...

But there is an easy way to perform serialization of these objects: use XAMLWriter and XAMLReader:

public class MyDependencyObject : DependencyObject
{
public static readonly DependencyProperty MyDependencyPropertyProperty =
  DependencyProperty.Register("MyDependencyProperty", 
	typeof(String), typeof(MyDependencyObject));
 
  public String MyDependencyProperty
  {
    get { return (String)GetValue(MyDependencyObject.MyDependencyPropertyProperty); }
    set { SetValue(MyDependencyObject.MyDependencyPropertyProperty, value); }
  }
 
}
 
...
 
MyDependencyObject obj = new MyDependencyObject();
obj.MyDependencyProperty = "One love";
String serializedString = XamlWriter.Save(obj);
Console.WriteLine(serializedString);

It will produce something like this:

<MyDependencyObject MyDependencyProperty="One love" 

	xmlns="clr-namespace:ANTOINE.Jonathan;assembly=ANTOINE.Jonathan" />
Shout it kick it on DotNetKicks.com

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

jmix90
Software Developer http://wpf-france.fr
France (Metropolitan) France (Metropolitan)
Jonathan creates software, mostly with C#,WPF and XAML.

He really likes to works on every Natural User Interfaces(NUI : multitouch, touchless, etc...) issues.



He is awarded Microsoft MVP in the "Client Application Development" section since 2011.


You can check out his WPF/C#/NUI/3D blog http://www.jonathanantoine.com.

He is also the creator of the WPF French community web site : http://wpf-france.fr.

Here is some videos of the projects he has already work on :

Comments and Discussions

 
QuestionAny easy solution for generics too? Pin
hans windhoff30-Sep-11 18:17
Memberhans windhoff30-Sep-11 18:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 25 Feb 2010

Tagged as

Stats

12.5K views