Silverlight tutorial Part 3: Use networking to retrieve data and fill the DataGrid

Source: Internet
Author: User
Tags xml format example
ArticleDirectory

This is the third part of the eight series of tutorials. This series demonstrates how to use beta1 of Silverlight 2 to build a simple Digg client application. These tutorials are intended to read in order and help explain some of the core programming concepts of Silverlight.

Use networking to retrieve Digg stories

Silverlight 2 has a built-in networking API that allows the Silverlight client to call remote rest, SOAP/WS *, RSS, JSON, and xml http Services. Silverlight 2 also includes the built-in Socket API (system. net. sockets), allowing the Silverlight client to communicate through non-http protocol (ideal for such scenarios as chat services ).

Cross-origin access

When a Silverlight 2 application performs a network call, it can always call back to their "Origin" servers (meaning they can call the URLs of the downloaded applications from the same domain ). Silverlight 2 applications can also make cross-domain network calls (meaning they can call URLs in different domains from the downloaded applications), as long as the remote web server has an XML policy file, indicates that the client allows these cross-origin calls.

Silverlight 2 defines an XML policy file format, allowing the server administrator to precisely control what access a client should have. Silverlight 2 also complies with the default flash cross-origin policy file format-this means that you can use Silverlight 2 to call any existing remote rest that allows cross-origin access of Flash clients on the web, soap/WS *, RSS, JSON, or XML endpoint (end-point ).

Digg.com has a set of very cool Digg APIs that communicates over HTTP. Because they have a flash Cross-Domain Policy file on their server, we can call them directly from our Silverlight Digg client application (instead of requiring us to access their APIs through our web server ).

Digg.com topic feed API

We want to allow end users to enter a Search topic (for example, "Programming") using our application, and then click the "search" button to retrieve the first n matching stories from digg.com:

We can use the digg.com list stories rest API feed API. It accepts a topic parameter (for example, get/stories/topic/programming) in the URL and returns a Digg story set that matches the XML format of the topic. Click here to see the XML format example.

Use System. net. WebClient to asynchronously call Digg rest feed

After the above search button is clicked, We will process its Click Event, retrieve the topic string to be searched from the watermarktextbox control, and then start a network call sent to Digg, retrieve the corresponding XML list for that topic.

Silverlight contains the WebClient helper class in the system. Net namespace (this class is also in the complete. NET Framework ). We can use this class to asynchronously download content from a URL. The advantage of asynchronous download of Digg stories is that our UI does not need to be blocked or become unresponsive while waiting for remote server replies (allowing us to have a very smooth user experience ).

To execute asynchronous download through the WebClient class, we need to register a downloadstringcompleted event processing method (which will be called after the requested content is downloaded) and then call WebClient. download by downloadstringasync (URL:

UseCodeNow we can asynchronously retrieve a string of XML data, containing Digg stories about any theme the user wants.

Use LINQ to XML to analyze Digg stories in XML format into story objects

Now, we can retrieve the XML segment of Digg story data. The next step is to analyze it (PARSE ), and convert it into the Diggory object that we can operate and bind to the control.

We will first define a digstory class, which has attributes that can be mapped to the XML content from Digg (we will use the new "automatic attribute" feature of C # To implement this ):

Then we can use LINQ (built in Silverlight 2) and LINQ to XML (an additional library included in our Silverlight application) to easily analyze and filter the XML documents returned from Digg, use the following code to translate it into a Diggory object sequence:

Note that we now have a strong type digstory object that we can operate from XML.

Display Digg stories in the DataGrid Control

We will use the new Silverlight DataGrid Control to display the Digg story in our application. To use it, we need to reference the Silverlight Data Control.ProgramSet, and then replace the "Todo" text on the previous page with a DataGrid Control declaration:

The DataGrid allows you to explicitly configure the column declaration and display type (to achieve maximum control), or you can set its autogeneratecolumns attribute to true to allow the DataGrid to use reflection on the data source, create a default column for you based on your object definition.

Then we can update our background code class and use a programming method to bind the itemsource attribute of the DataGrid to the sequence of stories retrieved from Digg when the search button is clicked:

Now, run our Silverlight application and perform a search. We will see a list of real-time topic story data retrieved from Digg:

The Silverlight DataGrid supports all the standard functions that the client grid control should have as expected: bidirectional in-situ editing, selection, scrolling, changing the table column size, and so on. It also supports automatic flow la S, meaning it can dynamically expand or contract to fill the content containers that contain it. The DataGrid also has a rich template model that allows you to customize the display and table column data editing. I will write more posts in the future to discuss how to use the DataGrid.

The following steps

Now we can retrieve the Digg story data from digg.com and display the story data in our application.

The next step is to return to our page. XAML identifier and remove the style declaration in the row we are currently using.

Let's skip to the next Tutorial: Use style elements to better encapsulate the look and feel.

[Original address] Silverlight tutorial Part 3: Using networking to retrieve data and populate a DataGrid
[Original article] Friday, February 22,200 AM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.