从JSON中获取数据,存储在IsolatedStorageSettings.ApplicationSettings中,在

本文关键字:ApplicationSettings 存储 IsolatedStorageSettings 数据 JSON 获取 | 更新日期: 2023-09-27 18:27:46

我正在努力按照Windows Phone 8中标题所说的去做,下面是我的操作方法:

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;
    }

当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数据存储到存储中,然后在可用时立即检索,有什么想法吗?非常感谢您的帮助!

从JSON中获取数据,存储在IsolatedStorageSettings.ApplicationSettings中,在

您对await和async的思考方式是正确的,但您在页面加载时调用了另一个方法,从而破坏了它的概念。Wilfred Wee所说的也是错误的。

正确的方法是在App.xamls.cs中声明一个事件处理程序,如下所示:

public event EventHandler<bool> SettingsReady;

然后将Application_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-NOT WHEN LOADED)中,声明当数据真正准备好时要调用的回调函数:

    // 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();
    }
相关文章:
  • 没有找到相关文章