Duration
15 minutes
Goals
The primary goal of this lab is to modify an existing application to use the event-based async pattern to download search results from a web service.

Required assets
The provided Exercise 1 folder contains a subfolder named Start with a solution you will use as starting point. There is also a Completed folder with a solution you can use to check your work. Please make sure you have these folders before you begin.
Challenge
Update an existing application to use the event-based async pattern when searching for movies.
- Examine the start solution.
- Add a new method to MovieApi.cs called SearchAsync that uses
WebClient'sDownloadStringAsyncmethod and theDownloadStringCompletedevent. - In MovieApi.cs, create a public
Actionnamed SearchComplete and invoke it when the search completes, passing in the movie data. - In SearchPage.xaml.cs, update the OnSearch method to use the new SearchAsync method and SearchComplete action.
Steps
Examine the Start Solution
- Open the MediaPhone shared project.
- Inspect SearchPage.xaml; it displays a
SearchBarand aListView. - Open the Data folder in the solution explorer. The code in MovieApi.cs is used to query the iTunes movie database. SearchResult.cs holds a model object class to hold the returned data.
- Run the application and try searching for a movie.
- Perform a search using a keyword that will return multiple results (try captain). Try to interact with the UI before the search results are returned? What happened?
Add an Asynchronous Search Method
- Open MovieApi.cs.
- Create a new
voidmethod named SearchAsync that accepts a singlestringparameter named text. - Copy the null check code, the creation of the local query
stringand the creation of theWebClientfrom the old Search method. - Instead of calling the
WebClient'sDownloadStringmethod, subscribe to theDownloadStringCompletedEventHandler. You may use any subscription method you prefer; the completed solution uses a lambda expression. - Within the handler, check the
DownloadStringCompletedEventArgs'sErrorproperty fornull. - If
Erroris null, we can assume the download completed successfully. Save theDownloadStringCompletedEventArgs'sResultproperty to a localstringvariable named resultText. - Parse the resultText using the static
JsonConvert.DeserializeObjectmethod and save to local variable named results. - Call the
WebClientwc'sDownloadStringAsyncmethod. For the parameter, create a newUripassing query into the constructor of theUri.
Provide the Search Results
We'll create a public action that will provide the deserialized results to a subscriber when the results are ready.
- In MovieApi.cs, create a public
Actionthat accepts anIEnumerableofSearchItems and anExceptionnamed SearchComplete. - Add the
eventkeyword before theActiondeclaration to limit its accessibility.
The deserialized results variable contains a property that is also named results which is a - If
e.Erroris not null, invoke the SearchCompleteActionpassing innullfor the first parameter and the error for the second parameter.
List of SearchItems.
When invoking the Action, pass in results.results as the first parameter and null for 2nd Exception parameter.
Invoke the Async Search method
- Open SearchPage.xaml.cs.
- In the OnSearch method, delete the
try,catch, andfinallyblocks. - Create a local instance of
MovieApinamed api. Subscribe to its SearchCompleteeventthat we created in the last step. - In the handler, check if the
Exceptionexists, and if so return. - If the
Exceptionisnull, use aforeachloop to add each item from the passed inIEnumerableofSearchItems to theListView's data source named Data. - Set the searchProgress's IsRunning property to null to hide the activity indicator.
- After the handler, call api's SearchAsync method, passing in the search terms text from the searchBar.
- Run the application. Notice that the
ActivityIndicatoris now visible during searches.
Summary
In this exercise, you updated an existing application to use the event-based async pattern to query a web service for search results. This allowed the UI to remain responsive while waiting for the results from the web service.