开发者

Best practice for WP7 serializeable class [is that the way its done]

I am trying to get this basic class right, its supposed to do the following:

  • Have a list of string, were each second a new string is created.
  • Have a current string, which represents the current string (in this case last created)
  • Use an observable collection for data binding support

The code for the class looks like this, the whole project can be found at this link:

http://www.filesavr.com/XXRM3TJ9LSW6FEC

Any way to make this nicer, or is it "as good as it gets".

Thanks, Chris

PS: I know, not a real question, but if I will base a lot of classes on this design, so I want to be sure not to duplicate mistakes. I though about createing my own observable collection which supports "current" and serialization, but I struggle a little bit with the generic attribute. Would you create one, or use the approach I used in the example below?

[DataContract]
public class SerializerTest : INotifyPropertyChanged
{
    private DispatcherTimer _dT;
    private List<string> _strings;

    public static string Key { get{return typeof (SerializerTest).FullName;} }

    [DataMember]
    public List<string> Strings
    {
        get
        {
            return _strings;
        }
        set
        {
            _strings = value;
            StringsObservable = new ObservableCollection<string>();
            foreach (var s in _strings) StringsObservable.Add(s);
        }
    }


    [DataMember]
    public int CurrentStringIndex { get; set; }


    public ObservableCollection<string> StringsObservable { get; set; }
    public string CurrentString
    {
        get
        {
            if (Strings == null) return null;
            if (Strings.Count <= CurrentStringIndex) return null;
            return Strings[CurrentStringIndex];
        }
    }

    public SerializerTest()
    {
        Strings = new List<string>();
        StringsObservable = new ObservableCollection<string>();
        InteralInit();
    }

    [OnDeserialized]
    public void Init(StreamingContext c)
    {
        InteralInit();
    }

    private void InteralInit()
    {
        _dT = new DispatcherTimer();
        _dT.Tick += (a, b) => AddString();
        _dT.Interval = new TimeSpan(0, 0, 0, 2);
        _dT.Start();
    }

    public void AddString()
    {
        Strings.Add(DateTime.Now.ToLongTimeString() + ":" + DateTime.N开发者_运维知识库ow.Millisecond);
        StringsObservable.Add(Strings.Last());

        CurrentStringIndex = Strings.Count - 1;
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(""));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}


Binary serialization has proven to be much faster than data contract serializer, so you may want to consider that option instead. Kevin Marshall has a great post on this: http://blogs.claritycon.com/kevinmarshall/2010/11/03/wp7-serialization-comparison/


You might find our articles on serializing to binary on Windows Phone 7 useful:

http://verysoftware.co.uk/blog/serializing-to-binary-on-wp7-part1/
http://verysoftware.co.uk/blog/serializing-to-binary-on-wp7-part2/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜