c# - Serializing a class containing a WPF Brush using `DataContractSerializer` -
i have class serializing like:
using (filestream filestreamwriter = new filestream(filename, filemode.create)) { var datacontractserializer = new datacontractserializer(typeof(classtobeserialized)); datacontractserializer.writeobject(filestreamwriter, btchartgrouplist); filestreamwriter.close(); }
this works fine until add property of type brush
(called areabrush
) classtobeserialized
class. areabrush
property solidbrush
, lineargradientbrush
or radialgradientbrush
. during serialization, datacontractserializer
throws:
type 'system.windows.media.matrixtransform' data contract name 'matrixtransform:http://schemas.datacontract.org/2004/07/system.windows.media' not expected. consider using datacontractresolver if using datacontractserializer or add types not known statically list of known types - example, using knowntypeattribute attribute or adding them list of known types passed serializer.
any ideas on how can work? played around brushconverter
didn't have luck that.
i guess add 3 types of brushes properties hoping better solution.
edit: of vmaleev ended doing this:
[ignoredatamember] public brush areabrush { { return _areabrush; } set { setproperty(ref _areabrush, value, () => areabrush); } } [datamember] public string areabrushtext { { using (stringwriter sw = new stringwriter()) { xamlwriter.save(areabrush, sw); string s = sw.tostring(); return sw.tostring(); } } set { areabrush = (brush)xamlreader.parse(value); } }
you cannot simply. recommend mark brush property [xmlignore] , serialize , deserialize separately using xamlwriter , xamlreader:
// example of writing using (var outfile = file.createtext("brush.xaml")) { xamlwriter.save(brush, outfile); } // example of reading using (stream s = file.openread("brush.xaml")) { brush b = xamlreader.load(s); }
view this topic more information
Comments
Post a Comment