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;
}
}
}
}
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;
}
}
}
}