Windows 8 Application Development-asynchronous call

Source: Internet
Author: User

Whether it is a desktop client or a Web application, operations that take a long time are usually processed. In order not to affect the interaction experience between users and applications during this period of time, developers usually use the asynchronous call technology, in this way, complicated logic operations are performed asynchronously. Users can continue to use applications without waiting for response.

This article uses a simple example to demonstrate how to use asynchronous programming in Windows 8 applications. First, compile a "Get Blogs" button and click it to retrieve the Blog list from the Windows Blog. Of course, the process of obtaining blog information is asynchronous. In this process, in order to test whether the user can still interact with the application, we design another "Change Text" to modify the content of 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, add a Click event for the "Get Blogs" button. The difference between getBlogs_Click and the previous Click event is that an async keyword is added. Async indicates that the following content should be implemented through asynchronous methods. The method obtains the Blog content through SyndicationClient. RetrieveFeedAsync, and notifies the application to call asynchronous operations through the await operator without affecting normal user interaction. If the asynchronous call is not used, the user can continue to use the application only after all the Blog content is loaded.

private async void getBlogs_Click(object sender, RoutedEventArgs e){    waitingText.Text = "Loading Blogs...";    SyndicationClient client = new SyndicationClient();    client.BypassCacheOnRetrieve = true;    Uri feedUri = new Uri("http://blogs.windows.com/windows/b/bloggingwindows/atom.aspx");    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();    }}
Demo

Run the program and click the "Get Blogs" button. Now the application has obtained the Blog content asynchronously. You can click "Change Text" to verify whether the user can continue to use other functions.

After you click "Get Blogs", "Loading Blogs…" appears ..." It indicates that the asynchronous call has been started.

Click "Change Text" before obtaining the Blog content. The Text will Change to "Please Waiting ...", It indicates that the user can still interact with the application during asynchronous calls.

After the asynchronous call is Completed, the text part is updated to "Completed !".

Now, the development of asynchronous calls is complete. This article is only one type of asynchronous calling. Of course, there are many other types of APIs available for everyone, including C #, VB, and JS multi-language development.

Source code

Http://sdrv.ms/XCAv6K

Related Article

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.