Question

I developping in Windows Phone 7.1, I have to do some web services calls to get a json string, I use the following code (that is into a class):

public void GetRegions()
        {
            if (!_wc.IsBusy)
            {
                _wc.DownloadStringAsync(new Uri("http://lapiazzashopping.it/test/mobile/getRegions.php"));
                _wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted_Regions);
            }
        }

        private void wc_DownloadStringCompleted_Regions(object sender, DownloadStringCompletedEventArgs e)
        {
            Debug.WriteLine("Web service says: " + e.Result);
            Regions = JsonConvert.DeserializeObject<IList<Regions>>(e.Result);
        }

Now after this call I want to change page that show the result from get from json. If I start the call by a button and this button is the same for the page change the compiler changes the page but the call is not yet completed because I cannot manage the methode "wc_DownloadStringCompleted_Regions" This methode is calle authomatically by the compiler at the end of the call, so my issue is that I cannot know when the call is completed and I cannot know when change the page at the right moment.

Is there any solution that can permit me to "pause" the code until the call and deserializion is completed??

Thanks

Was it helpful?

Solution

Add the eventhandler (_wc.DownloadStringCompleted) before the web call.

Also since you are calling an async method you should be doing

await _wc.DownloadStringAsync(....

Otherwise it's not async at all, and the method should be written with the async keyword. I think you need to read up on your async knowledge.

Here's a good guide to go through: http://msdn.microsoft.com/en-us/library/hh191443.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top