Decorating serializable class with extra properties
I'm building a Windows Phone 7 application and I'm trying to decorate a generated class with an additional property to bind against, but I'm a bit puzzled on how to solve this architecturally. What I currently have is this class, which is generated with the xsd.exe tool from an XML file:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepT开发者_高级运维hroughAttribute]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class Session
{
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Abstract { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Speaker { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute]
public string TimeslotBegin { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute]
public string Location { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute]
public string TimeslotEnd { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute]
public string Title { get; set; }
}
I am fetching the XML from the web and deserializing this with a XmlSerializer
, but I want to add an additional property to allow the user to "flag" items in the UI. I want to be able to bind to this property, so it should notify the UI thread when changed.
Any ideas on how to solve this?
For this situation, I'd recommend you separate your Model from your ViewModel.
The ViewModel is a data representation (including bindable properties) specifically designed for your UI.
The Model is the "pure" data representation, specifically designed for modelling your domain and for persistance (either directly to IsolatedStorage or perhaps persisted via a web service)
So, my recommendation is that you build some ViewModel classes for your UI to bind to - and then work out how these ViewModels interact with the Model.
As an aside, I'd also be cautious about using the XSD generated classes within Windows Phone 7 - WP7 seems to prefer the XDocument Linq XML classes, rather than the XmlDocument XML classes (but I may have this wrong!)
精彩评论