2013-08-15 2 views
0

나는 제목이 그것에 대해 이동하는 방법 윈도우 폰 8 말한다 여기에 무엇을 할 노력하고있어를 검색 할 때 데이터를 검색, JSON, IsolatedStorageSettings.ApplicationSettings에 저장 데이터를 가져 오기 :JSON이

private async void Application_Launching(object sender, LaunchingEventArgs e) 
{ 
    var settings = IsolatedStorageSettings.ApplicationSettings; 
    settings.Add("listofCurrency", await CurrencyHelpers.getJsonCurrency()); 
} 

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);   
    } 
} 
다음 MainPage로드, 나는 즉시 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; 

    } 

아이디어는 JSON 데이터를 스토리지에 저장 한 다음 즉시 사용할 수있을 때 아이디어를 가져오고 싶습니다. 도움을 많이 받으실 수 있습니다!

+0

(10)는 내가 동안 (사용하여 시도! settings.Contains ("listofCurrency") 그러나 다만 사이클 while 루프를 통해 스레드 결코 완료하지 않는 JSON 데이터를 얻을 수있는 기다려온 방법. –

답변

0

await와 async로 생각하는 방식은 정확하지만 페이지가로드 될 때 다른 메소드를 호출하여 개념을 파괴했습니다. Wilfred Wee가 말한 것은 또한 잘못된 것입니다.

올바른 방법은 다음과 같이 당신의 App.xamls.cs에 이벤트 핸들러를 선언하는 것입니다 :

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

public event EventHandler<bool> SettingsReady;

는 그런 다음에 Application_Launching() 방법을 변경

이제 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(); 
    } 
+0

안녕하세요 @ 조지, 도와 줘서 고마워! 이벤트를 정적으로 만들어야했지만 그렇지 않으면 모든 것이 완벽했습니다. 다시 한번 감사드립니다. –