Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / WPF
Tip/Trick

Serialize DependencyObject : it's easy !

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
25 Feb 2010CPOL 15.8K   2   2
You often read on the web that the DependencyObjects are not marked as serializable and that this is a major drawback of them...But there is a easy way to perform a serialization of these object : use XAMLWriter and XAMLReader :public class MyDependencyObject : DependencyObject{public...
You often read on the web that the DependencyObjects are not marked as serializable and that this is a major drawback of them...

But there is a easy way to perform a serialization of these object : use XAMLWriter and XAMLReader :
C#
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" /> :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
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

 
GeneralReason for my vote of 2 useless Pin
Jason Ti23-Nov-10 1:17
Jason Ti23-Nov-10 1:17 
GeneralReason for my vote of 5 ok Pin
caongocnam29-Jun-10 2:54
caongocnam29-Jun-10 2:54 

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.