title: Prepare data before go to the main view
body: <p>When the user touch the app icon, 
  I want do these steps before user go to the main view</p>
  
  <ol>
  <li>Fetch json string from URI</li>
  <li>Use JArray.Parse to get the value</li>
  <li>After all finish, go to the main view.</li>
  </ol>
  
  <p>The problem is how can I prevent user to go to the main view
  and put all the code<br>
  I tried to put it in Application_Launching method in the App.xaml.cs file</p>
  
  <pre><code>// Code to execute when the application is launching (eg, from Start)
  // This code will not execute when the application is reactivated
  private void Application_Launching(object sender, LaunchingEventArgs e) 
  {
      // code here
  }
  </code></pre>
  
  <p>But it doesn't prevent the program to go to the main view before the fetching finished.<br>
  And I found that actually in the MainPage.xaml, if I put this code like this</p>
  
  <pre><code>protected override void OnNavigatedTo(NavigationEventArgs e)
  {
      while(true) {} 
      // it will prevent the program to go to the main view, 
      // and will stick with the loading screen until this function reach its end
  }
  </code></pre>
  
  <p>So I think, I can put the all the code here, when I finish the fetch, I will just break  the while and it will go to the main view automatically.
  <br><br>
  And I try, this is the code</p>
  
  <pre><code>protected override void OnNavigatedTo(NavigationEventArgs e)
  {
      bool isFetchFinished = false;
  
      ObservableCollection&lt;PromoViewModel&gt; Promos = new ObservableCollection&lt;PromoViewModel&gt;();
  
      WebClient client = new WebClient();
  
      client.DownloadStringCompleted += (s, evt) =&gt;
      {
          if (evt.Error == null)
          {
              // Retrieve the JSON
              string jsonString = evt.Result;
  
              JArray promos = JArray.Parse(jsonString);
              foreach (JObject promo in promos)
              {
                  string name = promo["name"].Value&lt;string&gt;();
                  string description = promo["description"].Value&lt;string&gt;();
                  string img = promo["image"].Value&lt;string&gt;();
  
                  Promos.Add(new PromoViewModel() { Name = name, Description = description, Img = img });
              }
            isFetchFinished = true;
            System.Diagnostics.Debug.WriteLine("finish fetch");
         }
      };
  
      // run 
      client.DownloadStringAsync(new Uri("the json url")); 
  
      while(true) {
          if(isFetchFinished) {
              App.ViewModel.LoadData(Promos); // pass value to main view model
              break; // after complete, break
          }
      }
  }
  </code></pre>
  
  <p>I thought it would work, but it was not.<br>
  This is what I found,<br>
  The WebClient DownloadStringAsync won't run until the OnNavigatedTo function finished.<br>
  Because it's still waiting for the while loop to break and reach the end function.<br>
  And this</p>
  
  <pre><code>isFetchFinished = true; // will never executed
  </code></pre>
  
  <p>Resulting infinite loop. <br>
  I think I put the fetch code in the wrong method. Where is the right place to put all of this?</p>