Windows Phone uses async CTP to asynchronously load data

Source: Internet
Author: User

Async CTP is a good thing, that is, it is difficult to install...

Conflict with N multi-patch, so long as the installation order is correct, it can be installed smoothly...

Can refer to the previous written: http://www.cnblogs.com/sun8134/archive/2011/09/19/2181030.html

 

Asynchronization is a good thing. It can make the front-end respond when the background data is operated, rather than getting stuck.

After async CTP is installed, asyncctplibrary_phone.dll can be referenced in the WP project to implement asynchronous loading Using async and await.

Ms has encapsulated some asynchronous operations, such as WebClient which can be directly used:

Uri ui = new Uri(urls);
System.Net.WebClient wc1 = new System.Net.WebClient();
string txs = await wc1.DownloadStringTaskAsync(ui);

 

The following describes how to use async and await to asynchronously load data.

The purpose of the instance is to load an XML file and bind the data to the ListBox.

First, I randomly found the XML of a student's score.

 

Create a data class students. CS

 

Then edit the binding template of ListBox:

 

Then we need a progressindicator, so we can give the user a prompt when loading the data. I am a little lazy and have been using a ready-made item.

Let's take a look at the author's introduction on how to use this stuff:

Http://www.jeff.wilcox.name/2011/07/creating-a-global-progressindicator-experience-using-the-windows-phone-7-1-sdk-beta-2/

Http://www.cnblogs.com/alexis/archive/2011/09/04/2165994.html

Approximate effect:

 

The front-end is almost everything.

Below are the code events in code behind.

First, we need to write a method to read data from XML.

        private ObservableCollection<students> loaddata()
        {
            XDocument xdoc = XDocument.Load("data.xml");
            ObservableCollection<students> studentsdataCollection = new ObservableCollection<students>();
Foreach (xelement element in xdoc. element ("Student transcript"). descendants ("Student Score "))
            {     
                studentsdataCollection.Add(new students()
                {
Stu_no = element. element ("student ID"). value,
Stu_name = element. element ("name"). value,
Stu_english = element. element ("Foreign Language"). value,
Stu_mathematic = element. element ("Advanced Mathematics"). value,
Stu_physics = element. element (""). value,
Stuno_politics = element. element ("political"). value,
Stu_sports = element. element ("Sports"). value,
Stu_total = element. element ("Total"). Value
                });
            }
            //Thread.Sleep(5000);
            return studentsdataCollection;
        }

 

Use async and await in the page loading event to call this method asynchronously and bind the data to the ListBox.

        private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
Globalindicator. instance. busyforwork ("loading data. Please wait ...");
            ObservableCollection<students> d1=await Task<ObservableCollection<students>>.Factory.StartNew(new Func<ObservableCollection<students>>(loaddata));
            base.Dispatcher.BeginInvoke(() =>
            {
                listbox1.ItemsSource = d1;
            });
 
            GlobalIndicator.Instance.WorkDone();
        }

 

In fact, it is very simple. Let's take a look at the approximate effect (Because reading XML is faster, so I added thread. Sleep (5000); because it is asynchronous loading, we can switch between consumer items normally during the loading process ):

 

For more information, see:

Http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

Http://www.wintellect.com/CS/blogs/jgarland/archive/2011/04/18/new-for-windows-phone-now-the-async-ctp.aspx

Http://www.cnblogs.com/cracker/archive/2012/08/13/WindowsPhone_AsyncCTP.html#2448086

Http://blogs.msdn.com/ B /kevinash/archive/2012/02/21/async-ctp-task-based-asynchronous-programming-for-windows-phone.aspx

Http://blogs.msdn.com/ B /lucian/archive/2012/03/25/asyncctp-installation-problems-and-vs11.aspx

Http://blogs.msdn.com/ B /lucian/archive/2011/05/20/talk-how-to-write-an-async-connected-app-for-windows-phone-7.aspx

 

 

Complete instance:

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.