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
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