Consuming Live Services in Windows Phone 7


Windows Phone 7 is a mobile operating system developed by Microsoft, and is the successor to the Windows Mobile platform. It is primarily aimed at the consumer market rather than the enterprise market. Here, we will focus on developing for Windows Phone 7. The Windows Phone Application Platform enables developers to create engaging consumer experiences running on a Windows Phone. It is built upon existing Microsoft tools and technologies such as Visual Studio, Expression Blend, Silverlight, and the XNA Framework. Developers already familiar with those technologies and their related tools will be able to create new applications for Windows Phone without a steep learning curve.

This article focuses on how to consume Live Services in a Windows Phone 7 application. We will use Microsoft Visual Studio 2010, Bing API, and Windows Phone Developers tools to create the sample application. The Bing application programming interface (API) enables developers to programmatically submit the queries to, and retrieve the results from the Bing Engine. The Version 2 of the Bing API offers the support for multiple protocols.

So, let's start with developing the application.

1. Open Visual Studio 2010 and create new Windows Phone 7 Application. Navigate to File->New->Project->Windows Phone 7 Application.

2. We will need to work with various XML classes, so, add a reference to the System.Xml.Linq namespace.To do this, right-click the application in Solution Explorer, click Add Reference, and then, click System.Xml.Linq.

3. Now, Open the MainPage.xaml file and add a few rows and columns to the Grid named LayoutRoot.

1.png

4. Then, add two text blocks to display the title of the application and the sample text to search the topic.

5. After this, add a ComboBox control with three Combo Box items. Then, set the ComboBoxItems Content property to the values of the items to be searched. Here, lets, for example, add Microsoft, Windows Phone and Silverlight. You can add items as per your need.


3.png

6. Next, add a button and also register a click event handler to it. On the click of this button, the search takes place and the results are displayed.

4.png

7. After this, add a ListBox control. This will display a list of the results of the search. Also, add an ItemTemplate that will bind the TextBlocks in the list to the Title and URL of the search.

5.png

8. Now, let's navigate to the MainPage.xaml.cs file. In this file, add a reference to the System.Xml.Linq namespace with the using statement.

6.png

9. Next, let's create a class named SearchResult that will store the Title, URL, and other properties of the result.

7.png

10. Then, create an object of the string class that will store the URL. The URL contains an AppId that enables access to the Bing web service. You can create this application ID by logging in with valid Windows Live ID credentials in the Bing Development Center.

11. After this, create an object of the WebClient class. This class provides the common methods for sending the data to and receiving the data from a resource identified by an URI.

8.png

12. Now, in the constructor, set the default selected index of the combo box to zero.

13. Then, create an OpenReadCompleted event handler. This event occurs when an asynchronous operation to open a stream containing a resource completes.

9.png

14. Now, let's handle the click event of the button. In this, get the value of the current value selected in the combo box.

10.png

15. Then, call the OpenreadAsync() method and also pass the URI created earlier. This method opens a readable stream containing the specified resource.

11.png

16. Now, let's handle the OpenReadCompleted event handler. In this, create an object of the XElement class. This class represents an Xml element.


12.png

17. Then, using the OpenReadCompletedEventArgs class object, check if some error occurred, in which case then simply return.

13.png

18. If no error has occurred, then create a list of strings. Also, create an object of the XNamespace class. This class represents the XML Namespace. Then, set its value to the URL to search the Bing Service.

14.png

19. Next, create an object of the XElement class. This class represents an Xml element.

20. Load() method to get the result in the form of xml. Also, pass the Result property of the OpenReadCompletedEventArgs class object.

21. After this, using the SearchResult class created earlier, write a LINQ query to get the Title, URL, and other properties from the Result.

15.png

22. Then, set the DataContext property of the list box to the result of the query.

16.png

23. Now, run the application. A combo box with some default items is displayed. Also, a button is displayed.


17.png

24. Now, select one of the items to be searched from the combo box

25. A list of the relevant URL and description related to the topic is displayed.


18.png

This is a simple application that demonstrates how to develop a Windows Phone 7 application that consumes the Live Bing services. Similarly, you can develop an application to consume the other services including Bing Maps and Twitter.


Similar Articles