win8 Metro Style: 如何在工作线程中更新UI界面

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Xml.Dom;
using Windows.UI.Xaml;
using Windows.UI.Core;


namespace Models
{
    public class DataEngine
    {
        const string channelUri = "http://7516803.sinaapp.com/CouchPotato/ChannelListSatellite.xml";
        CoreDispatcher SampleDispatcher;


        public DataEngine()
        {
            SampleDispatcher = Window.Current.CoreWindow.Dispatcher;
            GetAllChannels();
        }


        public  event Windows.UI.Core.InvokedHandler ListChangedEventHandler;


        private Channels _ChannelList;
        public Channels ChannelList
        {
            get
            {
                if(_ChannelList == null)
                {
                    return new Channels();
                }
                return _ChannelList;
            }
            set
            {
                _ChannelList = value;
                if (ListChangedEventHandler != null)
                {

 // 在挂接ListChangedEventHandler这个事件的位置 在UI线程 会更新UI线程的元素, 或者ChannelList是绑定给UI元素的。
                    SampleDispatcher.InvokeAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                    (s, a) =>
                    {
                        ListChangedEventHandler(s, a);
                    }, 
                    this, null);

               
                }
            }
        }


        public void GetAllChannels()
        {
            // perform the search
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(channelUri));
            request.BeginGetResponse(new AsyncCallback(ReadCallback), request);


        }
        /// <summary>
        /// A callback that receives the search results
        /// </summary>
        private void ReadCallback(IAsyncResult asynchronousResult)
        {
          HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
          HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
          using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
          {
            string resultString = streamReader1.ReadToEnd();


            XmlDocument doc = new XmlDocument();
            doc.LoadXml(resultString);
            //XmlNodeList channelList = doc.SelectNodes("/root/channel/name");


            XmlNodeList channelList = doc.GetElementsByTagName("name");


            Channels chs = new Channels();
            foreach (IXmlNode node in channelList)
            {
                ChannelItem it = new ChannelItem();
                chs.Add(it);
                it.Title = node.FirstChild.NodeValue.ToString();
            }
            ChannelList = chs;
          }
        }


  
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值