Windows Phone 8 Can't Update listbox item in XML file after using hardware back button? -
hi have list box , saving list box selected item in xml file thats working fine problem when close app , reopen , add more value list box previous value removed form xml file , listbox how can save current added value , previous value in xml file using following code :
and iam using following code
<grid x:name="contentpanel" grid.row="2" margin="15,10,15,0"> <listbox name="list_location" tap="list_location_tap" foreground="black"> <listbox.itemtemplate> <datatemplate> <textblock x:name="item_name" text="{binding description, mode=oneway}" padding="5,15,5,15" textwrapping="wrap" fontsize="{staticresource phonefontsizelarge}"/> </datatemplate> </listbox.itemtemplate> </listbox> <listbox name="list_locationadd" background="red" foreground="black" visibility="collapsed"> <listbox.itemtemplate> <datatemplate> <textblock x:name="item_name" text="{binding description, mode=oneway}" padding="5,15,5,15" textwrapping="wrap" fontsize="{staticresource phonefontsizelarge}"/> </datatemplate> </listbox.itemtemplate> </listbox> </grid> and end code follow:
xmlwritersettings x_w_settings = new xmlwritersettings(); x_w_settings.indent = true; using (isolatedstoragefile isf = isolatedstoragefile.getuserstoreforapplication()) { using (isolatedstoragefilestream stream = isf.openfile(filename, filemode.create)) { xmlserializer serializer = new xmlserializer(typeof(observablecollection<prediction>)); using (xmlwriter xmlwriter = xmlwriter.create(stream, x_w_settings)) { data.add(new prediction() { description = app.professionalid }); list_locationadd.itemssource = data; serializer.serialize(xmlwriter, data); } } } protected override void onnavigatedto(navigationeventargs e) { try { if (list_locationadd != null) { using (isolatedstoragefile isf = isolatedstoragefile.getuserstoreforapplication()) { using (isolatedstoragefilestream str = isf.openfile(filename, filemode.open)) { xmlserializer serializer = new xmlserializer(typeof(observablecollection<prediction>)); observablecollection<prediction> data = (observablecollection<prediction>)serializer.deserialize(str); if (list_locationadd != null) { this.list_locationadd.itemssource = data; list_locationadd.visibility = visibility.visible; } } } } } catch (exception ex) { } }
change in code per below :
using (isolatedstoragefile isf = isolatedstoragefile.getuserstoreforapplication()) { using (isolatedstoragefilestream stream = isf.openfile(filename, filemode.create)) { xmlserializer serializer = new xmlserializer(typeof(observablecollection<prediction>)); using (xmlwriter xmlwriter = xmlwriter.create(stream, x_w_settings)) { data.add(new prediction() { description = app.professionalid }); list_locationadd.itemssource = data; serializer.serialize(xmlwriter, data); } } update in current code :
isolatedstoragefile isostore = isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.assembly, null, null); if (isostore.fileexists(filename)) { try { using (isolatedstoragefilestream isostream = new isolatedstoragefilestream(filename, system.io.filemode.open, system.io.fileaccess.read, isostore)) { xmlserializer serializer = new xmlserializer(typeof(observablecollection<prediction>)); observablecollection<prediction> data = (observablecollection<prediction>)serializer.deserialize(isostream); this.list_locationadd.itemssource = data; } } catch(exception ex) { } } else { try { using (isolatedstoragefilestream isostream = new isolatedstoragefilestream(filename, filemode.create, isostore)) { xmlserializer serialize = new xmlserializer(typeof(observablecollection<prediction>)); using (xmlwriter writer = xmlwriter.create(isostream, x_w_settings)) { data.add(new prediction() { description = app.professionalid }); list_locationadd.itemssource = data; serialize.serialize(writer, data); } } } catch (exception ex) { } } hope helpful you.
Comments
Post a Comment