c# - Get data from JSON, store in IsolatedStorageSettings.ApplicationSettings, retrieve data when JSON is retrieved -
i'm trying title says in windows phone 8 , here how go it:
private async void application_launching(object sender, launchingeventargs e) { var settings = isolatedstoragesettings.applicationsettings; settings.add("listofcurrency", await currencyhelpers.getjsoncurrency()); }
in currencyhelpers:
public async static task<dictionary<string, double>> getjsoncurrency() { httpclient client = new httpclient(); string jsonresult = await client.getstringasync("http://openexchangerates.org/api/latest.json?app_id=xxxxxxx"); jsoncurrency jsondata = jsonconvert.deserializeobject<jsoncurrency>(jsonresult); dictionary<string, double> currencycollection = new dictionary<string, double>(); currencycollection = jsondata.rates; return currencycollection; }
when mainpage loads, call method currencyhelpers:
public static keyvaluepair<double, double> getstoragecurrencypairrates(string firstcurrency, string secondcurrency) { var settings = isolatedstoragesettings.applicationsettings; double firstcurrencyrate = 1; double secondcurrencyrate = 1; dictionary<string, double> currencycollection = new dictionary<string,double>(); //needs code here check if "listofcurrency" has jsondata stored in it. settings.trygetvalue<dictionary<string,double>>("listofcurrency", out currencycollection); foreach (keyvaluepair<string, double> pair in currencycollection) { if (pair.key == firstcurrency) { firstcurrencyrate = pair.value; } else if (pair.key == secondcurrency) { secondcurrencyrate = pair.value; } } return new keyvaluepair<double, double>(firstcurrencyrate, secondcurrencyrate); } }
the idea want store json data storage, retrieve when available, ideas? appreciated!
the way thinking await , async correct destroyed concept calling method when page loads. wilfred wee said wrong.
the correct way declare event handler in app.xamls.cs this:
public event eventhandler<bool> settingsready;
then change application_launching() method following:
private async void application_launching(object sender, launchingeventargs e) { var settings = isolatedstoragesettings.applicationsettings; settings.add("listofcurrency", await currencyhelpers.getjsoncurrency()); if (settingsready!= null) settingsready(this, true); }
and in mainpage.xaml.cs (constructor - not when loaded) declare call function called when data ready:
// constructor public mainpage() { initializecomponent(); // call functions app.settingsready += app_settingsready; } void app_settingsready(object sender, bool e) { // here call function further data processing getstoragecurrencypairrates(); }
Comments
Post a Comment