Windows Phone 8 append to JSON file -


i'm working on windows phone 8 app. i'm having issue appending json file.
works fine if keep app open once close , come in starts writing beginning of file.

relevant code:

private async void btnsave_click(object sender, routedeventargs e) {     // create entry , intialize values textbox...     gasinfoentries _entry = null;     _entry = new gasinfoentries();     _entry.gallons = txtboxgas.text;     _entry.price = txtboxprice.text;     _gaslist.add(_entry);      //txtblockpricepergallon.text = (double.parse(txtboxgas.text) / double.parse(txtboxprice.text)).tostring();      // serialize our product class string         string jsoncontents = jsonconvert.serializeobject(_gaslist);      // app data folder , create or open file storing json in.                 storagefolder localfolder = applicationdata.current.localfolder;     storagefile textfile = await localfolder.createfileasync("gasinfo.json", creationcollisionoption.openifexists); //if await operator error add async class (btnsave)      //open file     using (irandomaccessstream textstream = await textfile.openasync(fileaccessmode.readwrite))     {         //write json string         using (datawriter textwriter = new datawriter(textstream))         //using (datawriter textwriter = new datawriter(textstream))         {             textwriter.writestring(jsoncontents);             await textwriter.storeasync(); //writes buffer store         }     } }  private async void btnshow_click(object sender, routedeventargs e) {     storagefolder localfolder = applicationdata.current.localfolder;     try     {         // getting json file if exists, or file not found exception if not         storagefile textfile = await localfolder.getfileasync("gasinfo.json");          using (irandomaccessstream textstream = await textfile.openreadasync())         {             //read text stream             using (datareader textreader = new datareader(textstream))             {                 //get size ...not sure  think check file size (lenght) based on next 2 commands waits until read                 uint textlength = (uint)textstream.size;                 await textreader.loadasync(textlength);                 //read                 string jsoncontents = textreader.readstring(textlength);                 // deserialize gas info                 _gaslist = jsonconvert.deserializeobject<list<gasinfoentries>>(jsoncontents) list<gasinfoentries>;                  displaygasinfoentries();              }         }     }     catch     {         txtshow.text = "something went wrong";     }       }  private void displaygasinfoentries() {     txtshow.text = "";     stringbuilder gasstring = new stringbuilder();     foreach (gasinfoentries _entry in _gaslist)     {         gasstring.appendformat("gallons: {0} \r\n price: ${1} \r\n", _entry.gallons, _entry.price); // think /r/n means return , new line...{0} , {1} calls "variables" in json file     }     txtshow.text = gasstring.tostring(); } 

thanks

do call btnshow_click each time you've started app? because otherwise _gaslist empty; if call btnsave_click previous made changes lost.

so please make sure, restore saved json data before add items _gaslist.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -