C# 从JSON获取数据,存储在IsolatedStorageSettings.ApplicationSettings中,在检索JSON时检索数据
我正在努力做到Windows Phone 8中标题所说的,以下是我的做法:C# 从JSON获取数据,存储在IsolatedStorageSettings.ApplicationSettings中,在检索JSON时检索数据,c#,windows-phone-8,C#,Windows Phone 8,我正在努力做到Windows Phone 8中标题所说的,以下是我的做法: private async void Application_Launching(object sender, LaunchingEventArgs e) { var settings = IsolatedStorageSettings.ApplicationSettings; settings.Add("listofCurrency", await CurrencyHelpers.getJsonCurr
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;
}
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);
}
}
public异步静态任务getJsonCurrency()
{
HttpClient=新的HttpClient();
string jsonResult=await client.GetStringAsync(“http://openexchangerates.org/api/latest.json?app_id=xxxxxxx");
JSONCurrency jsonData=JsonConvert.DeserializeObject(jsonResult);
Dictionary currencyCollection=新建字典();
currencyCollection=jsonData.Rates;
退币托收;
}
当主页加载时,我立即从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;
}
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);
}
}
public static KeyValuePair getStorageCurrencyPairRates(字符串firstCurrency,字符串secondCurrency)
{
var设置=隔离存储设置。应用设置;
双第一货币兑换率=1;
二次货币兑换率=1;
Dictionary currencyCollection=新建字典();
//这里需要一些代码来检查“listofCurrency”中是否已经存储了JSONData。
settings.TryGetValue(“货币列表”,out currencyCollection);
foreach(currencyCollection中的KeyValuePair对)
{
if(pair.Key==firstCurrency)
{
firstCurrencyRate=pair.Value;
}
else if(pair.Key==secondCurrency)
{
secondCurrencyRate=pair.Value;
}
}
返回新的KeyValuePair(firstCurrencyRate、secondCurrencyRate);
}
}
我的想法是将JSON数据存储到存储器中,然后在可用时立即检索,有什么想法吗?非常感谢您的帮助 您使用wait和async的思维方式是正确的,但是您在页面加载时调用了另一个方法,从而破坏了它的概念。威尔弗雷德·韦所说的也是错误的 正确的方法是在App.xamls.cs中声明事件处理程序,如下所示:
公共事件事件处理程序设置就绪代码>
然后将应用程序_Launching()方法更改为以下内容:
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);
}
现在,在MainPage.xaml.cs(构造函数-不在加载时)中,声明要在数据实际准备就绪时调用的回调函数:
// Constructor
public MainPage()
{
InitializeComponent();
// Call back functions
App.SettingsReady += App_SettingsReady;
}
void App_SettingsReady(object sender, bool e)
{
// Here call your function for further data processing
getStorageCurrencyPairRates();
}
我尝试使用while(!settings.Contains(“listofCurrency”)但是线程只是循环通过while循环,等待的方法获取JSON数据从未完成。嗨@George,非常感谢您的帮助!我不得不将事件设置为静态,但在其他方面一切都很好。再次感谢。