c# - using DataContractSerializer to store Data in Isolatedstorage -


i working on windows phone application having listbox image & text block. here image located in isolatedstorage of application.to display image in listblock i'm converting stream. after using datacontractserializer serialize & store in isolatedstorage.

code in page1:

public bookdatalist listobj = new bookdatalist();  public class bookdatalist : list<bookdata>//for storing bookdata class items type of list { }        [datacontract] public class bookdata {     [datamember]     public bitmapimage imagebinary     {         { return m_imagebinary; }         set { m_imagebinary = value; }     }      [datamember]     public bitmapimage m_imagebinary;      public bookdata(string strimagename)     {         //*** image binary ***'         bitmapimage image = new bitmapimage();         isolatedstoragefile isostore = isolatedstoragefile.getuserstoreforapplication();         string isofilename = strimagename;         stream stream = isostore.openfile(isofilename, system.io.filemode.open);         image.setsource(stream);         this.imagebinary = image;         stream.close();     }      [datamember]     public string bookname { get; set; }      [datamember]     public string bookpath { get; set; }      [datamember]     public string folderpath { get; set; } } 

here adding items list datatype :

            listobj.add(new bookdata(coverimageforlib) { bookname = mytitle.value,   bookpath = item.path, folderpath = split[1] }); 

here storing file in isolated storage :

  if (settings1.fileexists("mystoreitems"))   {       settings1.deletefile("mystoreitems");   }   using (isolatedstoragefilestream filestream = settings1.openfile("mystoreitems", filemode.create))   {       datacontractserializer serializer = new datacontractserializer(typeof(bookdatalist));       serializer.writeobject(filestream, listobj);   }   navigationservice.navigate(new uri("/library.xaml", urikind.relative)); 

here storing values. deserialize values in /library.xaml file mystoreitems

edit:

i got relevant here

  1. your "bookdata" class not have public parameterless constructor. cause error when attempting reload data.

  2. bitmapimage class not serializable.

in order work around these 2 problems need write custom serialization code, can adding interface: "ixmlserializable" class , implementing getschema, readxml, , writexml methods.

i getting system.reflection.targetinvocationexception exception while serializing listobj.

if code confusing, please ask me questions can clarify.

edit 1:

[datacontract] public class bookdata {     [datamember]     public string bookname { get; set; }     [datamember]     public string creator { get; set; }     [datamember]     public string bookpath { get; set; }     [datamember]     public string bookcoverpath { get; set; }     [datamember]     public string folderpath { get; set; }     [datamember]     public bitmapimage image { get; set; } }  public class bookdatalist : list<bookdata>//for storing bookdata class items type of list {  }   

i serialized & stored bookcoverpath string. whenever trying deserialize giving exception. not familiar datacontractserializer. can did wrong.

 // here getting data file in isolated storage     if (settings1.fileexists("mystoreitems"))         {             using (isolatedstoragefilestream filestream = settings1.openfile("mystoreitems", filemode.open))             {                 datacontractserializer serializer = new datacontractserializer(typeof(bookdata));                 listobj1 = (bookdatalist)serializer.readobject(filestream);             }         }     foreach (var item in listobj1)     {         string imagepath = item.bookcoverpath;         item.image = bookdata(imagepath);     }     listbox.itemssource = listobj1;//binding isolated storage list data      public bitmapimage bookdata(string imagepath)     {         //*** image binary ***'         bitmapimage image = new bitmapimage();         isolatedstoragefile isostore = isolatedstoragefile.getuserstoreforapplication();         string isofilename = imagepath;         stream stream = isostore.openfile(isofilename, system.io.filemode.open);         image.setsource(stream);         stream.close();          return image;     } 

the bookdata class has implemented logic load bitmapimage. if want persist / serialize class in xml, strip code away simple dto. won't give trouble serialize , save. serialize imagename. won't need custom xml serializer @ all.

to bitmap, load bitmap after deserialize xml. can eighter use imagename resolve bitmap within view load image url. or create bitmap property of bookdata (but marked [ignoredatamember]). after deserialize element, load bitmap isolatedstorage in second step , set bitmap on bookdata.

[datacontract] public class bookdata {     [datamember]     public string bookcoverpath { get; set; }      [ignoredatamember]     public bitmapimage image { get; set; } }   public bookdatalist loadlist() {   var booklist = loadanddeserializefromxml();   return booklist == null      ? null      : this.withbitmaps(booklist ); }  private bookdatalist withbitmaps(bookdatalist bookdata) {   bookdata.bookdata.foreach(b =>   {     b.image= loadpicture(b.bookcoverpath );   });                  return bookdata; } 

due abstraction , testing recomend you, implemented loadanddeserializefromxml() , loadpicture(string path) in repository class. not viewmodel/codebehind itself.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -