Asynchronous calls for Windows 8 application development

  
Regardless of whether the desktop client or the web application usually has a long processing time, in order to not affect the interaction between the user and the application during this time, the developer usually uses the asynchronous calling technology to make the more complicated logical operations. By asynchronous, the user can still continue to use the application without waiting for a response.
This article will demonstrate how to use asynchronous programming in Windows 8 applications with a simple example. First, let's write a "Get Blogs” button, click on it to get a list of blogs from the Windows Blog. Of course, the process of obtaining blog information is performed asynchronously. In this process, in order to test the user, the application 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=" ;40" Width="200"
Style="{StaticResource BasicTextStyle}"/>
<ListView x:Name="blogList" ItemTemplate="{StaticResource listTemplate}"< Br>VerticalAlignment="Top" HorizontalAlignment="Left" Height="550"
Margin="50,10,0,0" Width="650"/>
</StackPanel>
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
<TextBlock x:Name="waitingText" Height="40" 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="150" Click="changeText_Click" />
</StackPanel>
</StackPanel>
Next “Get Blogs” button to add Click event, getBlogs_Click and the previous Click event is different from an async keyword, see Async to indicate that the following content should be implemented by asynchronous methods. In the method, the content of the blog is obtained through SyndicationClient.RetrieveFeedAsync, and the await operator is used to inform the application to invoke the asynchronous operation without affecting the normal interaction of the user. If you do not use an asynchronous call, the user can only wait for all blog content to be loaded before continuing to use the app.
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>();< Br>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(); } }
Demonstration
Running Click & ldquo; Get Blogs & rdquo; button, and now has been obtained by application asynchronously Blog content, which we can click on the & ldquo; Change Text & rdquo; to verify whether the user can continue to use other functions should be.
Clicking on “Get Blogs” will appear with the words "Loading Blogs…", indicating that the asynchronous call has been initiated.

Click '“Change Text” before getting the blog content, the text will change to “Please Waiting…”, indicating that the user can still interact with the application when called asynchronously.

After the asynchronous call is completed, the text portion is updated to “Completed!”.

At this point, the development of asynchronous calls is complete. This article is just a type of asynchronous call, of course, there are many other types of APIs for everyone to use, and includes C#, VB, JS multi-language development.


Copyright © Windows knowledge All Rights Reserved