话不多少,今天要写一个小项目。我们写项目要做好准备。我们要做项目分析。要知道用户需求,然后在根据需求来规划自己的项目。我们要用自己所学,做最好的程序。尽自己所能完成项目需求。精简代码!

  我们今天要写的项目叫网络电视精灵。首先我们要做到的是要把我们事先写好的XML文件解析好放到TreeView空间上显示。,并选择一个Treeview的节点显示其对应的信息到DateGridView上!

 

  有几个细节要求写到再说!下面有彩蛋!!!

做好的窗体是这个样子的!

启动是这个样子的 

所有的类是这个样子的

 

每个类的代码和注释

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chap_网络电视精灵
{
   /// <summary>
   /// 电视频道父类,抽象类
   /// </summary>
    public abstract class ChannelBase
    {
        //这是一堆自动属性,很简单就是要来接收Xml文件的信息的!
        public string ChannelName { get; set; }
        public string  Path { get; set; }
        public string ChannelType { get; set; }
        public List<TvProgram> list { get; set; }
        public abstract void Fetch();
        //数据类型为List<TvProgram>的单列泛型集合
        private List<TvProgram> ProgramList = new List<TvProgram>();

        public List<TvProgram> ProgramList1
        {
            get { return ProgramList; }
            set { ProgramList = value; }
        }
       
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chap_网络电视精灵
{
    public class ChannelFactory
    {
        /// <summary>
        /// 工厂类
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ChannelBase CreateChannel(string type)
        {
            ChannelBase channel = null;
            switch(type)
            {
                case "TypeA":
                    channel = new TypeAChannel();
                    break;
                case "TypeB":
                    channel = new TypeBChannel();
                    break;
                default:
                    break;

            }
            return channel;
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Chap_网络电视精灵
{
    /// <summary>
    /// 节目管理类
    /// </summary>
    class ChannelManager
    {
        //单列泛型集合
        private List<ChannelBase> list = new List<ChannelBase>();

        public List<ChannelBase> List
        {
            get { return list; }
            set { list = value; }
        }
        //解析XML文件的方法
        public void ResolveChannels()
        {
            //1.1 我将解析好的Data中转存放到泛型集合
            //List<自定义类>

            XmlDocument doc = new XmlDocument();
            doc.Load("FullChannels.xml");
            //找根节点
            XmlNode root = doc.DocumentElement;
            foreach (XmlNode item in root.ChildNodes)
            {
                //一个item代表什么  Channel
                ChannelBase channel = Factory.CreateFactory(item["channelType"].InnerText);
                channel.ChannelType = item["channelType"].InnerText;
                channel.ChannelName = item["tvChannel"].InnerText;
                channel.Path = item["path"].InnerText;

                //归属到一个集合中
                list.Add(channel);
            }
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chap_网络电视精灵
{
    /// <summary>
    /// 简单工厂类
    /// </summary>
    class Factory
    {
        //一个静态的返回值为ChannelBase类型的方法
        public static ChannelBase CreateFactory(string type)
        {
            ChannelBase types = null;
            switch (type)
            {
                case "TypeA":
                    types = new TypeAChannel();
                    break;
                case "TypeB":
                    types = new TypeBChannel();
                    break;
            }

            return types;
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chap_网络电视精灵
{
    /// <summary>
    /// 电视频道类
    /// </summary>
    public class TvProgram
    {
        //节目名称
        public string ProgramName { get; set; }
        //时段
        public string Meridien { get; set; }
        //节目路径
        public string Path { get; set; }
        //时间
        public DateTime PlayTime { get; set; }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Chap_网络电视精灵
{
    /// <summary>
    /// A类频道类
    /// </summary>
    class TypeAChannel : ChannelBase
    {
        //重写父类的抽象方法,解析XML文件并绑定到泛型集合上
        public override void Fetch()
        {

            XmlDocument doc = new XmlDocument();
            doc.Load("北京电视台.xml");
            XmlNode root = doc.DocumentElement;
            //找根节点  
            foreach (XmlNode item in root.ChildNodes)//遍历子节点
            {
                if(item.Name.Equals("tvProgramTable"))//判断条件是节点的Name是否是"tvProgramTable"
                {
                    foreach (XmlNode items in item.ChildNodes)//遍历子节点的子节点
                    {
                        TvProgram Tp = new TvProgram();
                        Tp.PlayTime = Convert.ToDateTime(items["playTime"].InnerText);
                        Tp.Meridien = items["meridien"].InnerText;
                        Tp.Path = items["path"].InnerText;
                        Tp.ProgramName = items["programName"].InnerText;
                        ProgramList1.Add(Tp);//绑定到集合
                    }
                }
                
            }
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Chap_网络电视精灵
{
    class TypeBChannel : ChannelBase
    {
        //解析XML文件的方法
        public override void Fetch()
        {
            //1.1 我将解析好的Data中转存放到泛型集合
             //List<自定义类>
             XmlDocument doc = new XmlDocument();
            doc.Load("凤凰卫视.xml");
            //找根节点  
            XmlNode root = doc.DocumentElement;
            foreach (XmlNode item in root.ChildNodes)//遍历子节点
            {
                if (item.Name.Equals("ProgramList"))//判断条件是节点的Name是否是"tvProgramTable"
                {
                    foreach (XmlNode items in item.ChildNodes)//遍历子节点的子节点
                    {
                        TvProgram Tp = new TvProgram();
                        Tp.PlayTime = Convert.ToDateTime(items["playTime"].InnerText);
                        Tp.Path = items["path"].InnerText;
                        Tp.ProgramName = items["name"].InnerText;
                        ProgramList1.Add(Tp);//绑定到集合
                    }
                }

            }
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Chap_网络电视精灵
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        bool flag = false;
        private void FrmMain_Load(object sender, EventArgs e)
        {
            Insert();
            flag = true;
        }
//绑定TreeView的数据 public void Insert() { TreeNode minenode = new TreeNode(); minenode.Text = "我的电视台"; //bind tvList.Nodes.Add(minenode); //根节点 TreeNode root = new TreeNode(); root.Text = "所有电视台"; //bind tvList.Nodes.Add(root); ChannelManager manager = new ChannelManager(); manager.ResolveChannels(); List<ChannelBase> list = manager.List; foreach (ChannelBase item in list) { TreeNode tn = new TreeNode(); tn.Text = item.ChannelName; tn.Tag = item; root.Nodes.Add(tn); } } private void tvList_AfterSelect(object sender, TreeViewEventArgs e) { if(flag) {
//让右键选择菜单分开显示 if (tvList.SelectedNode.Level == 0) { ctms.Items[0].Visible = false; ctms.Items[1].Visible = false; } else if (tvList.SelectedNode.Parent.Text == "我的电视台") { ctms.Items[0].Visible = true; ctms.Items[1].Visible = false; } else if (tvList.SelectedNode.Parent.Text == "所有电视台") { ctms.Items[0].Visible = false; ctms.Items[1].Visible = true; } TreeNode selectNode = tvList.SelectedNode; if (selectNode.Tag == null) { return; } ChannelBase channel = (ChannelBase)selectNode.Tag; //给LIst集合填充数据 channel.Fetch(); List<TvProgram> list = channel.ProgramList1; clear(list); } } public void clear(List<TvProgram> list) { dgvList.AutoGenerateColumns = false; dgvList.DataSource = new BindingList<TvProgram>(list); } private void dgvList_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void 加入我的电视台ToolStripMenuItem_Click(object sender, EventArgs e) {
//右键加入我的电视台 ChannelBase channel = (ChannelBase)tvList.SelectedNode.Tag; TreeNode node = new TreeNode(); node.Text = channel.ChannelName; node.Tag = channel; tvList.Nodes[0].Nodes.Add(node); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { //删除所选中的节点 XmlDocument doc = new XmlDocument(); doc.Load("FullChannels.xml"); //找根节点 () XmlNode root = doc.DocumentElement; foreach (XmlNode item in root.ChildNodes) { if (item["tvChannel"].InnerText == tvList.SelectedNode.Text) { tvList.SelectedNode.Remove(); break; } } } } }

  我是彩蛋!!! 

  所有代码都在上面了,最后总结一下思路!我们首先把电视频道的XML文件解析到单列泛型集合里,然后把集合的数据绑定到TreeView控件上并绑定Tag值为ChannelBase对象;然后通过选中的节点的Tag值拿到电视节目解析完的泛型集合来绑定DateGridView!

 

posted on 2016-09-06 16:18  北漂-少年  阅读(12423)  评论(5编辑  收藏  举报