⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 how to use RSS services on windows mobile
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO;

namespace LiveQnA3
{
    /// <summary>
    /// This sample application takes its ideas from the MSDN Magazine article about RSS feeds on SmartPhones by John Papa.
    /// As an extention to the original article, this sample application can store subscriptions to RSS feeds. When the application
    /// is started, all feeds that we subscribed to are re-initialized.
    /// 
    /// This sample application makes use of asynchronous calls to the websites of which RSS feeds are retrieved.
    /// The application does not automatically refresh feeds every now and then.
    /// </summary>
    /// <remarks>
    /// The article and the corresponding source code can be found here:
    /// http://msdn.microsoft.com/msdnmag/issues/06/12/DataPoints/
    /// </remarks>
    public partial class MainForm : Form
    {
        private delegate void RefreshListDelegate(DataSet ds);

        private List<Uri> rssFeeds;

        public MainForm()
        {
            InitializeComponent();
            rssFeeds = new List<Uri>();
        }

        private void menuItemMaintainFeeds_Click(object sender, EventArgs e)
        {
            NewFeedForm addFeed = new NewFeedForm();
            if (addFeed.ShowDialog() == DialogResult.OK)
            {
                // add Uri to the list of RSS feeds and get contents.
                rssFeeds.Add(addFeed.Feed);
                BeginGetFeed(rssFeeds[rssFeeds.Count - 1]);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string filePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\RssReader.txt";

            // Restore the list of RSS feeds we are subscribed to
            if (File.Exists(filePath))
            {
                using (StreamReader sr = new StreamReader(filePath))
                {
                    while (!sr.EndOfStream)
                    {
                        rssFeeds.Add(new Uri(sr.ReadLine()));
                        BeginGetFeed(rssFeeds[rssFeeds.Count - 1]);
                    }
                }
            }
        }

        private void MainForm_Closing(object sender, CancelEventArgs e)
        {
            // Save the list of RSS feeds when we close the application
            if (rssFeeds.Count > 0)
            {
                string filePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\RssReader.txt";

                using (StreamWriter sw = new StreamWriter(filePath, false))
                {
                    foreach (Uri u in rssFeeds)
                    {
                        sw.WriteLine(u.ToString());
                    }
                }
            }
        }

        private void BeginGetFeed(Uri url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            IAsyncResult asyncResult = request.BeginGetResponse(new AsyncCallback(EndGetFeed), request);
        }

        public void EndGetFeed(IAsyncResult result)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            XmlTextReader xmlRdr = null;
            DataSet ds = null;
            try
            {
                request = (HttpWebRequest)result.AsyncState;
                response = (HttpWebResponse)request.EndGetResponse(result);
                xmlRdr = new XmlTextReader(response.GetResponseStream());
                ds = new DataSet();
                ds.ReadXml(xmlRdr);
                response.Close();
                xmlRdr.Close();

                Invoke(new RefreshListDelegate(RefreshList), ds);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (response != null)
                    response.Close();
                if (xmlRdr != null)
                    xmlRdr.Close();
                if (ds != null)
                    ds.Dispose();
            }
        }

        private void RefreshList(DataSet ds)
        {
            foreach (DataRow channelRow in ds.Tables["channel"].Rows)
            {
                string title = channelRow["title"].ToString();
                int itemCount = int.Parse(channelRow.GetChildRows("channel_item").Length.ToString());

                string[] displayInfo = new string[2];
                displayInfo[0] = title;
                displayInfo[1] = itemCount.ToString();

                ListViewItem item = new ListViewItem(displayInfo);
                item.Tag = channelRow;
                listView1.Items.Add(item);
            }
        }

        private void menuItemShowFeed_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count == 1)
            {
                DataRow channelRow = listView1.Items[listView1.SelectedIndices[0]].Tag as DataRow;
                ItemListForm feedItems = new ItemListForm(channelRow);
                feedItems.ShowDialog();
            }
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -