开发者

Custom serialization in Caliburn.Micro 1.1

I'm upgrading a 1.0 WP7 application to CM 1.1. Among other stuff, I'm removing the old attribute-based tomb开发者_如何学JAVAstoning and implementing storage classes.

This typically involves creating a class for each VM for storage purposes, deriving it from StorageHandler<T> (where T is the type of the VM) and overriding its Configure method like e.g.:

public override void Configure()
{
  Property(x => x.SomeSerializableProperty).InPhoneState().RestoreAfterViewLoad();
  // ...
}

In this context, how can I implement a custom serialization mechanism using my own serialize/deserialize code for objects which could not be automatically serialized? For instance, one of my VM's has a StrokeCollection property and I'd like to serialize the strokes in it, but to this end I need to replace the default mechanism which would raise security exceptions.

Could anyone show a fake CM WP7 sample to illustrate how to customize the serialization of some property, so that I can place my own code for serializing/deserializing it? Thanks!


I don't know if this is the right path, but it works; here is a code sample:

Property(x => x.Strokes).InPhoneState().RestoreAfterViewReady().Configure(x =>
{
    x.Save = SaveStrokes;
    x.Restore = RestoreStrokes;
});

with their implementations like:

void SaveStrokes(BoardViewModel vm, Func<string> serialize, StorageMode nMode)
{
  IsolatedStorageSettings.ApplicationSettings[vm.DisplayName + "ThePropertyKey"] = 
  // ...get data from vm and serialize
}

and conversely:

void RestoreStrokes(BoardViewModel vm, Func<string> serialize, StorageMode nMode)
{
  // use IsolatedStorageSettings.ApplicationSettings[vm.DisplayName + "ThePropertyKey"] 
  // to check if the key exists, and if it is there get the serialized data and deserialize
}

As for strokes, I'm using my own serialization class as my usual tool for this purpose (SharpSerializer) seems having issues in restoring (it throws an ambiguous match reflection exception).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜