Click here to Skip to main content
14,033,076 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

11.7K views
Posted 25 Feb 2010
Licenced Ms-PL

Serialize DependencyObject: It's easy !

, 25 Feb 2010
Rate this:
Please Sign up or sign in to vote.
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 :

You may also be interested in...

Pro

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190423.1 | Last Updated 25 Feb 2010
Article Copyright 2010 by jmix90
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid