Async CTP是个好东西额,就是设置的时候可比坑…

跟N多补丁争辩,只要安装顺序对了才能顺风装上…

可以参照下从前写的:http://www.cnblogs.com/sun8134/archive/2011/09/19/2181030.html

 

异步是个好东西,可以让后台数据操作时前台也有影响而不是卡死

设置好Async
CTP后,只要在wp项目中引用AsyncCtpLibrary_Phone.dll就可以利用async和await来兑现异步加载了

MS已经封装好了一部分异步操作,比如WebClient里就能够直接选择:

Uri ui = new Uri(urls);

System.Net.WebClient wc1 = new System.Net.WebClient();

string txs = await wc1.DownloadStringTaskAsync(ui);

 

上边来介绍下哪些团结行使async和await来贯彻异步加载数据

实例中我们的目标是加载一个xml,并将数据绑定到listbox上来

先是我任由找了个学生成绩的XML

XML 1

 

接下来创设一个数码类students.cs

XML 2

 

下一场再编辑下listbox的绑定模板:

XML 3

 

下一场我们还必要一个ProgressIndicator,好在加载数据时给用户一个提醒,我相比偷懒从来在用别人现成的一个东东

XML 4

有关咋用这几个东东豪门去看小编的牵线吧:

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

大致效果:

XML 5

 

前台的东西大体差不离了

上面紧即使code behind里的代码事件了

先是大家要写一个办法来从xml中读取数据

        private ObservableCollection<students> loaddata()

        {

            XDocument xdoc = XDocument.Load("data.xml");

            ObservableCollection<students> studentsdataCollection = new ObservableCollection<students>();

            foreach (XElement element in xdoc.Element("学生成绩单").Descendants("学生成绩"))

            {     

                studentsdataCollection.Add(new students()

                {

                    stu_No = element.Element("学号").Value,

                    stu_Name = element.Element("姓名").Value,

                    stu_English = element.Element("外语").Value,

                    stu_Mathematic = element.Element("高等数学").Value,

                    stu_Physics = element.Element("大学物理").Value,

                    stuno_Politics = element.Element("政治").Value,

                    stu_Sports = element.Element("体育").Value,

                    stu_Total = element.Element("总分").Value

                });

            }

            //Thread.Sleep(5000);

            return studentsdataCollection;

        }

 

接下来在页面加载事件里用async和await来贯彻异步调用这些格局,并将数据绑定到listbox上

        private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)

        {

            GlobalIndicator.Instance.BusyForWork("正在加载数据,请稍候...");

            ObservableCollection<students> d1=await Task<ObservableCollection<students>>.Factory.StartNew(new Func<ObservableCollection<students>>(loaddata));

            base.Dispatcher.BeginInvoke(() =>

            {

                listbox1.ItemsSource = d1;

            });

 

            GlobalIndicator.Instance.WorkDone();

        }

 

实际很不难,看看大概效果(由于读取XML比较快,所以自己加了Thread.Sleep(5000);
由于是异步加载,所以加载进程中得以健康切换Pivot Item):

XML 6XML 7XML 8

 

约莫就在如此多,更加多新闻将看:

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

 

 

总体实例:

相关文章

网站地图xml地图