网络电视精灵
电视精灵
效果展示
该项目主要练习xml文件解析和面向对象思维;
1.电视台和节目信息都要从xml文件中解析,并绑定;
2.实现点击电视频道节点显示该频道的电视节目信息;
3.当时间到达用户设置的节目提醒时间后弹出窗口提醒用户节目即将开始;
效果演示
1.点击频道显示该频道的节目信息
2.选中全部电视台下的节点,右键显示右键菜单‘添加到我的电视台’;
3.选中我的电视台下的节点,右键显示右键菜单‘删除’;
4:右键电视节目信息显示设置提醒
5.点击提醒管理显示提醒窗体,右键提醒信息显示删除
xml文件
1.频道信息
<?xml version="1.0" encoding="utf-8" ?> <TVChannels> <Channel> <channelType>TypeA</channelType> <!--频道类型--> <tvChannel>北京电视台</tvChannel> <!--频道名称--> <path>files/北京电视台.xml</path> <!--频道对应XML文件的本地路径--> </Channel> <Channel> <channelType>TypeB</channelType> <tvChannel>凤凰卫视</tvChannel> <path>files/凤凰卫视.xml</path> </Channel> </TVChannels>
2.北京电视台
<?xml version="1.0" encoding="utf-8" ?> <typeA version =" 1.0"> <channelName>北京电视台</channelName> <tvProgramTable> <tvProgram> <playTime>2016-9-5 16:21</playTime> <!--节目播出时间--> <meridien>早间档</meridien> <!--时段--> <programName>重播:国际双行线</programName> <!--节目名称--> <path>c:\a.wma</path> <!--视频路径--> </tvProgram> <tvProgram> <playTime>2016-9-5 16:22</playTime> <!--节目播出时间--> <meridien>早间档</meridien> <!--时段--> <programName>北京您早</programName> <!--节目名称--> <path>C:\New Stories (Highway Blues).wma</path> <!--视频路径--> </tvProgram> <tvProgram> <playTime>2009-9-29 09:02</playTime> <!--节目播出时间--> <meridien>早间档</meridien> <!--时段--> <programName>重播《红星剧场》32集剧:勇者无敌(31)</programName> <!--节目名称--> <path>**</path> <!--视频路径--> </tvProgram> <tvProgram> <playTime>2009-9-29 09:58</playTime> <meridien>早间档</meridien> <programName>重播《红星剧场》32集剧:勇者无敌(32)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 11:15</playTime> <meridien>午间档</meridien> <programName>重播:专题片:我爱你,中国(14)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 11:58</playTime> <meridien>午间档</meridien> <programName>身边</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 12:30</playTime> <meridien>午间档</meridien> <programName>特别关注</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 13:05</playTime> <meridien>午间激情档</meridien> <programName>网络电视精灵</programName> <path>H:\video\01网络电视精灵.avi</path> </tvProgram> <tvProgram> <playTime>2009-9-29 13:40</playTime> <meridien>午间档</meridien> <programName>《玫瑰剧场》55集剧:闯关东中篇(21)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 14:32</playTime> <meridien>午间档</meridien> <programName>《玫瑰剧场》55集剧:闯关东中篇(22)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 15:27</playTime> <meridien>下午档</meridien> <programName>《玫瑰剧场》55集剧:闯关东中篇(23)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 16:19</playTime> <meridien>下午档</meridien> <programName>《玫瑰剧场》55集剧:闯关东中篇(24)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 17:31</playTime> <meridien>下午档</meridien> <programName>法治进行时</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 18:00</playTime> <meridien>晚间档</meridien> <programName>新闻晚高峰</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 18:30</playTime> <meridien>晚间档</meridien> <programName>北京新闻</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 18:56</playTime> <meridien>晚间档</meridien> <programName>天气预报</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 19:00</playTime> <meridien>晚间档</meridien> <programName>转播CCTV新闻联播</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 19:31</playTime> <meridien>晚间档</meridien> <programName>看气象</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 19:40</playTime> <meridien>晚间档</meridien> <programName>专题片:我爱你,中国(15)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 20:22</playTime> <meridien>晚间档</meridien> <programName>《爸妈都是老党员》首播庆典</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 21:21</playTime> <meridien>晚间档</meridien> <programName>《红星剧场》38集剧:咱爸咱妈六十年(1)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2009-9-29 23:25</playTime> <meridien>晚间档</meridien> <programName>重播文艺:花样年华歌舞大赛(5)</programName> <path>**</path> </tvProgram> </tvProgramTable> </typeA>
3.凤凰卫视
<?xml version="1.0" encoding="utf-8" ?> <typeB version =" 1.0"> <ProgramList> <Program> <playTime>2009-9-29 01:30</playTime> <!--播出时间--> <name>简单工厂模式</name> <!--节目名称--> <path>D:\简单工厂模式.wmv</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 02:10</playTime> <!--播出时间--> <name>非凡人物论成功(713)</name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 02:15</playTime> <!--播出时间--> <name>有报天天读(28/09/09) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 02:40</playTime> <!--播出时间--> <name>娱乐大风暴(682) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 03:15</playTime> <!--播出时间--> <name>上海新长宁集团特约之2009中华小姐环球大赛-美丽前奏(41) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 03:20</playTime> <!--播出时间--> <name>完全时尚手册:天桥云裳(632) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 03:40</playTime> <!--播出时间--> <name>东风康明斯凤凰冲击播:天力士特约之今日看世界(39) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 04:25</playTime> <!--播出时间--> <name>音乐中国风(442) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 04:35</playTime> <!--播出时间--> <name>维维健康新概念(193) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 04:40</playTime> <!--播出时间--> <name>红牛凤凰全球连线(28/09/09) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 05:05</playTime> <!--播出时间--> <name>红云红河时事开讲(28/09/09) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2009-9-29 05:30</playTime> <!--播出时间--> <name>别巡检调查队(34)(R) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> </ProgramList> </typeB>
代码:
1.频道类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 网络电视精灵 { public abstract class ChannelBase { /// <summary> /// 频道类 /// </summary> /// //频道名称 public string Name { get; set; } //频道节目集合 public List<TV> List { get; set; } //频道xml地址 public string Path { get; set; } //频道类型 public string Type { get; set; } public ChannelBase(string name,string path,string type) { this.Name = name; this.Path = path; this.Type = type; this.List = new List<TV>(); } public ChannelBase() { } public abstract void Fetch(); } }
2.TV类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 网络电视精灵 { public class TV { /// <summary> /// 电视节目类 /// </summary> /// //节目开始时间 public DateTime PlayTime { get; set; } //节目名称 public string ProgramName { get; set; } //节目地址 public string FilePath { get; set; } public TV() { } public TV( DateTime playtime,string name,string filepath) { this.PlayTime = playtime; this.ProgramName = name; this.FilePath = filepath; } } }
3.工厂类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 网络电视精灵 { class Factory { /// <summary> /// 工厂类 /// </summary> /// <param name="type"></param> /// <param name="name"></param> /// <param name="path"></param> /// <returns></returns> //简单工厂方法生成频道对象 public static ChannelBase create(string type, string name, string path) { ChannelBase cb = null; switch (type) { case "TypeA": cb = new BJ(name, path,type); break; case "TypeB": cb = new FH(name, path,type); break; } return cb; } } }
4.管理类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace 网络电视精灵 { class Management { //提醒类的静态集合,用于保存添加的提醒 public static List<Remind> Rlist = new List<Remind>(); //管理类方法,处理xml文件,返回处理后的频道对象的集合 public static List<ChannelBase> load() { List<ChannelBase> List = new List<ChannelBase>(); XmlDocument dic = new XmlDocument(); dic.Load("files\\FullChannels.xml"); XmlNode root = dic.DocumentElement; foreach (XmlNode item in root) { ChannelBase cb=Factory.create(item["channelType"].InnerText,item["tvChannel"].InnerText,item["path"].InnerText); cb.Fetch(); List.Add(cb); } return List; } //向提醒类集合添加对象的方法 public static bool remind(DateTime playtime, string ProgramName,DateTime reminddatetime) { bool flag =true; foreach (Remind item in Rlist) { if (item.ProgramName == ProgramName && item.Reminddatetime == reminddatetime) { flag = false; } } if (flag) { Remind r = new Remind(playtime, ProgramName, reminddatetime); Management.Rlist.Add(r); } return flag; } } }
5.北京电视台类
sing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace 网络电视精灵 { class BJ:ChannelBase { /// <summary> /// 北京电视台类 /// </summary> public BJ() { } public BJ(string name, string path,string type) : base(name,path,type) { } //解析北京电视台xml文件方法 public override void Fetch() { XmlDocument doc = new XmlDocument(); doc.Load(Path); XmlNode no = doc.DocumentElement; foreach (XmlNode item in no) { if (item.Name == "tvProgramTable") { foreach (XmlNode it in item) { TV tv = new TV(Convert.ToDateTime(it["playTime"].InnerText), it["programName"].InnerText, it["path"].InnerText); this.List.Add(tv); } } } } } }
6.凤凰卫视类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace 网络电视精灵 { class FH:ChannelBase { /// <summary> /// 凤凰卫视类 /// </summary> public FH() { } public FH(string name,string path,string type) : base(name,path,type) { } //解析凤凰卫视xml文件的方法 public override void Fetch() { XmlDocument doc = new XmlDocument(); doc.Load(Path); XmlNode no = doc.DocumentElement; foreach (XmlNode item in no.ChildNodes) { foreach (XmlNode it in item) { TV tv = new TV(Convert.ToDateTime(it["playTime"].InnerText),it["name"].InnerText,it["path"].InnerText); this.List.Add(tv); } } } } }
7.提醒类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 网络电视精灵 { class Remind:TV { /// <summary> /// 提醒类 /// </summary> /// //提醒时间 public DateTime Reminddatetime { get; set; } public Remind() { } public Remind(DateTime playtime, string ProgramName, DateTime reminddatetime) { this.PlayTime = playtime; this.ProgramName = ProgramName; this.Reminddatetime = reminddatetime; } } }
8.主窗体代码
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; namespace 网络电视精灵 { public partial class TvShow : Form { TreeNode root = null; public static Remindmg rmg; public TvShow() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //创建根节点 dataGridView1.AutoGenerateColumns = false; root = new TreeNode("我的电视台"); tree.Nodes.Add(root); info(); } //创建所有电视台的方法 private void info() { TreeNode tn = new TreeNode("所有电视台"); tree.Nodes.Add(tn); List<ChannelBase> list = Management.load(); foreach (ChannelBase item in list) { item.Fetch(); TreeNode tn1 = new TreeNode(item.Name); tn1.Tag = item; tn.Nodes.Add(tn1); } } private void tree_AfterSelect(object sender, TreeViewEventArgs e) { //显示右键菜单的方法 if (tree.SelectedNode.Tag != null) { dataGridView1.DataSource = ((ChannelBase)tree.SelectedNode.Tag).List; } foreach (TreeNode item in tree.Nodes) { foreach (TreeNode item1 in item.Nodes) { if (item1.Parent.Text == tree.Nodes[0].Text && item1 == tree.SelectedNode) { tree.ContextMenuStrip = contextMenuStrip1; } else if (item1.Parent.Text == tree.Nodes[1].Text && item1 == tree.SelectedNode) { tree.ContextMenuStrip = contextMenuStrip2; } else if (item == tree.SelectedNode) { tree.ContextMenuStrip = null; } } } } private void tree_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e) { } private void 加入我的电视台ToolStripMenuItem_Click(object sender, EventArgs e) { //将频道加入我的我的电视台的方法 bool flag = true; foreach (TreeNode item in tree.Nodes[0].Nodes) { if (item.Text == tree.SelectedNode.Text) { flag = false; } } if (flag) { TreeNode tn = new TreeNode(tree.SelectedNode.Text); tn.Tag = tree.SelectedNode.Tag; root.Nodes.Add(tn); } else { MessageBox.Show("已经添加到我的电视了","提示"); } } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { //删除我的频道的方法 tree.SelectedNode.Remove(); } private void toolStripMenuItem1_Click(object sender, EventArgs e) { //添加播放时提醒 DateTime Remind = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells[0].Value); Add(Remind); } private void Add(DateTime Remind) { //添加提醒的方法 DateTime playdatime = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells[0].Value); string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); if (Management.remind(playdatime, name, Remind)) { MessageBox.Show("添加成功", "提示"); updateRmg(); } else { MessageBox.Show("添加失败,已经添加过该提醒了", "提示"); } } //更新提醒列表的方法 public void updateRmg() { if (rmg != null) { rmg.Remindmginfo(); } } private void 提前十分钟提醒ToolStripMenuItem_Click(object sender, EventArgs e) { //提前十分钟提醒 DateTime Remind = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells[0].Value).AddMinutes(-10); Add(Remind); } private void 提前三十分钟提醒ToolStripMenuItem_Click(object sender, EventArgs e) { //提前三十分钟提醒 DateTime Remind = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells[0].Value).AddMinutes(-30); Add(Remind); } private void timer1_Tick(object sender, EventArgs e) { //到时提醒的方法 string time=DateTime.Now.ToString("yyyy-MM-dd hh:mm"); Remind r = null; foreach (Remind item in Management.Rlist) { if ((item.Reminddatetime.ToString("yyyy-MM-dd hh:mm")).Equals(time)) { MessageBox.Show(item.ProgramName+"马上就要开始了","提示",MessageBoxButtons.OK); r = item; } } if (r != null) { Management.Rlist.Remove(r); updateRmg(); } } private void 提醒管理ToolStripMenuItem_Click(object sender, EventArgs e) { //点击提醒管理打开提醒窗体 Remindmg from = new Remindmg(); from.Show(); } } }
9.提醒窗体代码
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; namespace 网络电视精灵 { public partial class Remindmg : Form { public Remindmg() { InitializeComponent(); } private void Remindmg_Load(object sender, EventArgs e) { //设置不显示未绑定的列 dataGridView1.AutoGenerateColumns = false; //获取提醒窗体对象 TvShow.rmg = this; Remindmginfo(); } //绑定数据源 public void Remindmginfo() { BindingSource bs = new BindingSource(); bs.DataSource = Management.Rlist; dataGridView1.DataSource = bs; } //删除的方法 private void 删除提醒ToolStripMenuItem_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { Remind r = null; string name = dataGridView1.SelectedRows[0].Cells["ProgramName"].Value.ToString(); string datetime = dataGridView1.SelectedRows[0].Cells["remindtime"].Value.ToString(); foreach (Remind item in Management.Rlist) { if (item.ProgramName == name && item.Reminddatetime.ToString().Equals(datetime)) { r = item; } } if (r != null ) { Management.Rlist.Remove(r); Remindmginfo(); } } } } }