How can I do this?
Tried using a TypeConverter, but the only thing I could think of was to construct the XML for the types, which doesn’t quite cut it. TypeConverters in xaml serialization will escape xml and treat it like plain text. Value converters aren’t much better.
Now, I’m moving to ISupportInitialize and will throw if changes are made after initialization, but I would have liked the immutable route…
Example of a type I wish to serialize:
public struct Foo
{
public string Bar {get;private set;}
public Foo(string bar) : this()
{
Bar = bar;
}
}
and the code
var foo = new Foo("lol");
var serializedFoo = XamlWriter.Save(foo);
Sorry, you can’t use normal serialization with immutable objects in XAML 2006.
With XAML 2009, which supports constructors and factory methods, you should be able to acomplish your goal. Just be warned, WPF 4 will not be using XAML 2009.