Whether it's a desktop client or a Web application that typically has lengthy processing operations, and in order to not affect the interaction between users and applications during this time, developers typically use asynchronous invocation techniques that allow more complex logical operations to be performed asynchronously, and users can still continue to use the application. There will be no response waiting for the situation to occur.
This article will show you how to use asynchronous programming in Windows 8 applications with a simple example. Let's start by writing a "Get Blogs" button that you can click to obtain a list of blogs from Windows blog. Of course, the process of getting the blog information is done asynchronously, in order to test the user can still interact with the application, we design a "change Text" to modify the content of the Waitingtext.
Code
<stackpanel orientation= "Horizontal" grid.row= "1" >
<StackPanel>
<textblock x:name= "ListTitle" height= "width=" "200"
Style= "{StaticResource Basictextstyle}"/>
<listview x:name= "Bloglist" itemtemplate= "{StaticResource ListTemplate}"
Verticalalignment= "Top" horizontalalignment= "left" height= "550"
Margin= "50,10,0,0" width= "650"/>
</StackPanel>
<stackpanel orientation= "Vertical" verticalalignment= "Top" >
<textblock x:name= "Waitingtext" height= "width=" "200"
Style= "{StaticResource Basictextstyle}"/>
<button x:name= "Getblogs" content= "Get Blogs" width= "150"
click= "Getblogs_click"/>
<button x:name= "Changetext" content= "Change Text" margin= "0,10,0,0"
Width= "click=" "Changetext_click"/>
</StackPanel>
</StackPanel>
Next to the "Get Blogs" button to add the Click event, Getblogs_click and the previous click event is different from a async keyword, see async to explain the following content to be implemented by asynchronous method. method to obtain the content of the blog through Syndicationclient.retrievefeedasync, and the await operator to tell the application to invoke the asynchronous operation, and does not affect the user's normal interaction. If an asynchronous call is not used, the user can only wait until all the blog content has been loaded to continue to use the application.
Private async void Getblogs_click (object sender, RoutedEventArgs e) {
Waitingtext.text = "Loading Blogs ...";
Syndicationclient client = new Syndicationclient ();
Client. Bypasscacheonretrieve = true;
Uri Feeduri = new Uri
Try
{
SyndicationFeed feed = await client. Retrievefeedasync (Feeduri);
observablecollection<blogitem> blogdata = new observablecollection<blogitem> ();
Listtitle.text = feed. Title.text;
foreach (SyndicationItem item in feed. Items)
{Blogdata.add (new Blogitem ()
{Author = Item. Authors[0]. Name.tostring (),
pubdate = Item. PublishedDate.Year.ToString () + "/" +
Item. PublishedDate.Month.ToString () + "/" +
Item. PublishedDate.Day.ToString (),
Title = Item. Title.text
}); }
Bloglist.itemssource = Blogdata;
Waitingtext.text = "completed!";
}
catch (Exception ex)
{
Waitingtext.text = "Can" t load the page: "+ ex." ToString (); } }
Demonstrate
Run the program click the "Get Blogs" button, now the application has been the asynchronous way to obtain the content of the blog, this is we can click "Change Text" to verify that users can continue to use the other features should be.
Clicking "Get Blogs" will show the words "Loading Blogs ...", which indicates that the asynchronous call has started.
Computer knowledge
Click "Change text" before getting to the content of the blog, and the text will become "please waiting ..." to show that the user can still interact with the application when the asynchronous call is made.
When the asynchronous call completes, the text part is updated to "completed!".
So far, the development of asynchronous calls is complete. This article is only a type of asynchronous call, of course, there are many other types of APIs for everyone to use, but also contains C #, VB, JS multi-language development.