Windows Phone 8 でタイトルに書かれていることを実行しようとしていますが、その方法は次のとおりです。
private async void Application_Launching(object sender, LaunchingEventArgs e)
{
var settings = IsolatedStorageSettings.ApplicationSettings;
settings.Add("listofCurrency", await CurrencyHelpers.getJsonCurrency());
}
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;
}
MainPage が読み込まれるとすぐに、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 some code here to check if "listofCurrency" already 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);
}
}
JSONデータをストレージに保存し、利用可能になったらすぐに取得したいという考えはありますか? 助けていただければ幸いです。