Click here to Skip to main content
15,904,416 members
Articles / All Topics

Serialize DependencyObject: It's easy !

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Feb 2010Ms-PL 14.1K   1
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:

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:

XML
<MyDependencyObject MyDependencyProperty="One love" 
	xmlns="clr-namespace:ANTOINE.Jonathan;assembly=ANTOINE.Jonathan" />
Shout it kick it on DotNetKicks.com
This article was originally posted at http://blog.lexique-du-net.com/index.php

License

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


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

 
QuestionAny easy solution for generics too? Pin
hans windhoff30-Sep-11 18:17
hans 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.