我无法序列化类的错误消息,因为无法序列化 SolidColorBrush
Error message that I cannot serialize a class because cannot serialize SolidColorBrush
该类有一个Brush的公共属性
The class has a public property of Brush
有修复吗?
比较复杂.我尝试使用可以序列化的支持属性问题是我还需要冻结 HighLight 以便我可以在 BackgroundWorker 上创建如果我为 HighLight 使用可序列化的支持属性,则 Highlight.Freeze 失败
It is more complex. I tried using a backing property that can be serialized Problem is that I also need to Freeze the HighLight so I can be created on a BackgroundWorker If I use a serializable backing property for HighLight then Highlight.Freeze fails
[Serializable()] public class WordIdLenHightlight : Object { private string highlightHex; public Int32 ID { get; private set; } public Byte Len { get; private set; } //[NonSerialized] public Brush Highlight { get; private set; } //{ if (string.IsNullOrEmpty(highlightHex)) return null; else return new SolidColorBrush((Color)ColorConverter.ConvertFromString(highlightHex)); } public string HighlightHex { get { return highlightHex; } } public override bool Equals(Object obj) { // Check for null values and compare run-time types. if (obj == null) return false; if (!(obj is WordIdLenHightlight)) return false; WordIdLenHightlight comp = (WordIdLenHightlight)obj; return (comp.ID == this.ID); } public override int GetHashCode() { return ID; } public WordIdLenHightlight(WordIdLenHightlight w) { ID = w.ID; Len = w.Len; Highlight = w.Highlight; highlightHex = w.Highlight.ToString(); Highlight.Freeze(); } public WordIdLenHightlight(Int32 id, byte len, Brush highlight) { ID = id; Len = len; Highlight = highlight; //highlightHex = Highlight.ToString(); Highlight.Freeze(); } public WordIdLenHightlight(Int32 id, byte len, string HighlightHex) { highlightHex = HighlightHex; ID = id; Len = len; Highlight = new SolidColorBrush((Color)ColorConverter.ConvertFromString(highlightHex)); Highlight.Freeze(); } } 推荐答案你不说你是如何序列化这个类的,但你可能可以通过以下方式来装饰 Brush 属性或其支持字段一个属性,例如 NonSerialized 或 XmlIgnore.
You don't say how you're serializing the class, but you can probably decorate the Brush property or its backing field by an attribute such as NonSerialized or XmlIgnore.
您可能想要序列化一些其他信息,而不是使您能够在反序列化时重建 Brush,例如,它的颜色.
You might want to serialize some other information than enables you to reconstruct the Brush on deserialization, for example, its color.
更多推荐
无法序列化 SolidColorBrush
发布评论