简体   繁体   中英

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.Now.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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM