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 operation more complicated. The logical operation is performed asynchronously, and 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}"

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 is “Get The Blogs” button adds the Click event. The difference between getBlogs_Click and the previous Click event is that there is an async keyword. When you see Async, the following content is implemented by the asynchronous method. 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>();

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

}); }

blogLis t.ItemsSource = blogData;

waitingText.Text = "Completed!";

}

catch (Exception ex)

{

waitingText.Text = "Cant load the page:" + ex.ToString(); } }

Demo

Run the program click “Get Blogs” button, now The app has already obtained the blog content asynchronously, which is something we can click on "Change Text" to verify that the user can continue to use other features.


After clicking "Get Blogs”, the words "Loading Blogs…" will appear, indicating that the asynchronous call has been initiated.



Computer Knowledge

Click on “Change Text” before getting the blog content, the text will change to “Please Waiting…”, Description Users can still interact with the app 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