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.
Right click on the computer icon on the desktop, select Manage, click on Services and Applications,
Method: first press the Windows + X combination shortcut to open the control panel and select "
1. Press the WIN+R shortcut to bring up the Run window, then t
System protection is similar to System Restore in that it periodically creates and
Win8 system super button "search" function usage
Solution for Metro applications that cannot be opened in Windows 8
How to adjust system demo mode attribute in Win8.1
Windows 8 always displays case lock and number lock icons on the desktop
How to add IE browser icon to Win8.1 desktop
Turn off Windows 8 touch keyboard to simulate real keyboard percussion
An effective solution to access a disk prompt to deny access
What should I do if Win8.1 fails to create a recovery drive?
The improper operation of the win8 system blue screen
Retrieving the Win8 Start Menu button
Boot Windows XP from a floppy disk
Win10 preview version 10061 login interface black screen complete solution
How to use the storm transcoding? Storm transcoding graphic tutorial
How to quickly replace the power mode of Win7 notebook
What should I do if the volume of the Win10 system cannot be adjusted?
Port mapping does not ask for people
Microsoft patches for Windows 8 Developer Preview