简单实现新闻阅读器(RSS)

  根据不同网站的RSS源获取数据(http请求),解析数据(XML),显示数据(绑定到集合),从而达到新闻阅读的效果。

  由于是WP初学者,MSDN参考信息有限,很多功能未能实现,比如RSS地址的解析,应该解析成.xml后缀的,但这里是直接输入的。

    

  

  在界面显示上,用到了Grid布局和DataTemplate模版套用

运行效果:

 

主要程序代码:

 private void btnShowNews_Click(object sender, RoutedEventArgs e)
        {
            //添加源
            UriBuilder uri = new UriBuilder(rssURL.Text);
            //初始化 请求
            HttpWebRequest feedRequest = WebRequest.Create(uri.Uri) as HttpWebRequest;

            if (feedRequest != null)
            { 
                //设置异步请求状态对象
                FeedUpdateState feedState = new FeedUpdateState();
                feedState.AsyncRequest = feedRequest;

                //异步请求
                feedRequest.BeginGetResponse(new AsyncCallback(HandleFeedResponse), feedState);
            }
        }

        private void HandleFeedResponse(IAsyncResult asyncResult)
        {
            if (asyncResult.AsyncState != null)
            {
                //获得异步请求的状态信息
                FeedUpdateState feedState = asyncResult.AsyncState as FeedUpdateState;
                HttpWebRequest feedRequest=feedState.AsyncRequest as HttpWebRequest;
                
                //结束异步请求
                try
                {
                    feedState.AsyncResponse = feedRequest.EndGetResponse(asyncResult) as HttpWebResponse;
                }
                catch (Exception)
                {
//由于HttpWebRequest异步调用完成时不能保证回调函数运行在 UI 线程,而更新TextBlock里的文本需在UI线程处理。Dispatcher类的BeginInvoke方法就是用来实现对 UI 线程的调用。
                    Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            MessageBox.Show("网路错误", "提示", MessageBoxButton.OK);
                        }));
                    return;
                }

                Stream streamResult;

                try
                {
                    //从状态对象中获取返回的数据流
                    streamResult = feedState.AsyncResponse.GetResponseStream();
                    if (streamResult != null)
                    {
          
                        //加载 xml中需要的元素
                        XElement xmlFeed = XElement.Load(streamResult);

                        Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            foreach (XElement curElement in xmlFeed.Descendants("item"))
                            {
                                NewsDetail tempNews = new NewsDetail();
                                tempNews.Title += (String)(curElement.Element("title")) + "\n";
                                tempNews.Time += (String)(curElement.Element("pubDate")) + "\n";
                                tempNews.Detail += (String)(curElement.Element("description")) + "\n";
                                tempNews.Link += (String)(curElement.Element("link")) + "\n";

                                AllNews.Add(tempNews);
                            }

                        }));
                    }
                    else
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            MessageBox.Show("获取数据为空", "提示", MessageBoxButton.OK);
                        }));
                    }
                }
                catch (Exception)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        MessageBox.Show("获取数据失败", "提示", MessageBoxButton.OK);
                    }));
                    return;
                }
            }

 

感慨:

最近 也一直在看杨中科老师的用wpf做的人事管理系统直播视频,才发现window phone的界面设计也是这么的似曾相识,想起了那句经典的微博

 

 

此项目参考"传智播客.net培训Windows 8开发视频教程"

源代码下载地址

 http://pan.baidu.com/share/link?shareid=173804&uk=1360555118

posted @ 2012-12-20 02:42  求小天  阅读(349)  评论(0编辑  收藏  举报