Error on serializing System.Windows.Media.Color

  • Thread starter Christopher Ireland
  • Start date
C

Christopher Ireland

Hello!

The following WPF code gives me an error:

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
BinaryFormatter bf = new BinaryFormatter();
bf.AssemblyFormat =
System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
bf.TypeFormat =
System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded;
byte[] bytes;
BigDeal bd = new BigDeal();
bd.Something = Colors.Red;
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, bd);
ms.Seek(0, 0);
bytes = ms.ToArray();
}
}
}

// serialized object
[Serializable]
public class BigDeal
{
Color something = Color.FromArgb(0, 0, 0, 0);

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public Color Something
{
get
{
return this.something;
}
set
{
this.something = value;
}
}
public BigDeal()
{
}
}
}

The error is:
System.Runtime.Serialization.SerializationException was unhandled
Message="Type 'System.Windows.Media.Color' in Assembly 'PresentationCore,
Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not
marked as serializable."
Source="mscorlib"

However, the same code in a WindowsFormApplication works fine:

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
BinaryFormatter bf = new BinaryFormatter();
bf.AssemblyFormat =
System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
bf.TypeFormat =
System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded;
byte[] bytes;
BigDeal bd = new BigDeal();
bd.Something = Color.Red;
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, bd);
ms.Seek(0, 0);
bytes = ms.ToArray();
}
}

}

// serialized object
[Serializable]
public class BigDeal
{
Color something = Color.FromArgb(0, 0, 0, 0);

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public Color Something
{
get
{
return this.something;
}
set
{
this.something = value;
}
}
public BigDeal()
{
}
}
}

Is not not possible to serialize properties of the type
System.Windows.Media.Color in WPF?

Many thanks for any insights!

--
Thank you,

Christopher Ireland
http://shunyata-kharg.doesntexist.com

"My education was dismal. I went to a series of schools for mentally
disturbed teachers."
Woody Allen
 
N

not_a_commie

Apparently it's not serializable. The DataContractSerializer help page
lists everything that is serializable as far as it's concerned. I
think you should just make a private get/set that converts that color
to/from an int. Mark that private property as serializable rather than
the Color property itself.
 
C

Christopher Ireland

not_a_commie,
Apparently it's not serializable. The DataContractSerializer help page
lists everything that is serializable as far as it's concerned. I
think you should just make a private get/set that converts that color
to/from an int. Mark that private property as serializable rather than
the Color property itself.

Thank you for your reply.

In my particular case that represents a lot of unnecessary work that I would
rather avoid. Why on earth would System.Windows.Media.Color be
unserializable, I wonder?

Ho-hum.

--
Thank you,

Christopher Ireland
http://shunyata-kharg.doesntexist.com

"The mind that does not understand is the Buddha. There is no other."
Ma-Tsu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top