市场上有许多电视软件,因此我们开发属于自己的电视软件!对于我们初学者来说也是拿来对付行外人吹牛的资本“^_^”
本套软件所用的技术:
01.C/S架构,数据存储在XML文件中
02.简单工厂模式
03.继承和多态
04.解析XML文档技术
05.深入剖析内存中数据的走向
06.TreeView控件的使用
首先你得创建以下这么多类!
namespace IntertnetTVS { //频道基类 pub
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Xml; 7 8 namespace IntertnetTVS 9 { 10 public class TypeAChannel : ChannelBase 11 { 12 public override void Fetch() 13 { 14 XmlDataDocument doc=new XmlDataDocument(); 15 doc.Load("北京电视台.xml"); 16 if (TVlist==null) 17 { 18 TVlist = new List<TVprogram>(); 19 } 20 XmlNode root = doc.DocumentElement; 21 22 foreach (XmlNode item in root.ChildNodes) 23 { 24 if (item.Name == "tvProgramTable") 25 { 26 foreach (XmlNode mynode in item.ChildNodes) 27 { 28 TVprogram tp = new TVprogram(); 29 tp.PlayTime = DateTime.Parse(mynode["playTime"].InnerText); 30 tp.MyProperty = mynode["meridien"].InnerText; 31 tp.Name = mynode["programName"].InnerText; 32 tp.Path = mynode["path"].InnerText; 33 TVlist.Add(tp); 34 } 35 } 36 } 37 38 } 39 } 40 }
lic abstract class ChannelBase { //频道节目名称 public string ChannelName { get; set; } //储存频道节目路径 public string Path { get; set; } //储存频道下的各个节目 public string Type { get; set; } //类型 public List<TVprogram> TVlist { get; set; } public abstract void Fetch(); } }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace IntertnetTVS 8 { 9 //节目类 10 public class TVprogram 11 { 12 //节目播出时间 13 public DateTime PlayTime { get; set; } 14 //节目播出时段 15 public string MyProperty { get; set; } 16 //节目名称 17 public string Name{ get; set; } 18 //节目保存路径 19 public string Path { get; set; } 20 21 } 22 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 8 //工厂类 9 namespace IntertnetTVS 10 { 11 public class ChannelFactory 12 { 13 //工厂来创建相应对象的简单工厂 14 public static ChannelBase CreateBase(string type) 15 { 16 ChannelBase bases = null; 17 switch(type) 18 { 19 case "TypeA": 20 bases=new TypeAChannel(); 21 break; 22 case "TypeB": 23 bases=new TypeBChannel(); 24 break; 25 } 26 return bases; 27 } 28 } 29 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Xml; 7 8 namespace IntertnetTVS 9 { 10 public class ChannelManager 11 { 12 public ChannelManager() 13 { 14 fullChannels = new Dictionary<string, ChannelBase>(); 15 } 16 //创建dictionary来储存所有的频道 17 private Dictionary<string, ChannelBase> fullChannels; 18 public Dictionary<string, ChannelBase> FullChannels 19 { 20 get { return fullChannels; } 21 set { fullChannels = value; } 22 } 23 //解析xml文档将数据储存到dictionary 24 public void ParseXmlToDictionary() 25 { 26 XmlDataDocument doc = new XmlDataDocument(); 27 doc.Load("FullChannels.xml"); 28 XmlNode root = doc.DocumentElement; 29 foreach (XmlNode item in root.ChildNodes) 30 { 31 //获取type的类型 32 string type = item["channelType"].InnerText; 33 //将类型传到工场中返回对应子类 34 ChannelBase mybase = ChannelFactory.CreateBase(type); 35 mybase.ChannelName = item["tvChannel"].InnerText; 36 mybase.Path = item["path"].InnerText; 37 mybase.Type = type; 38 FullChannels.Add(mybase.ChannelName, mybase); 39 } 40 } 41 } 42 }
窗体装载代码
1 sing System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace IntertnetTVS 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void Form1_Load(object sender, EventArgs e) 21 { 22 chishi(); 23 ChannelManager manager = new ChannelManager(); 24 manager.ParseXmlToDictionary(); 25 Dictionary<string, ChannelBase> ydy = manager.FullChannels; 26 foreach (var item in ydy.Values) 27 { 28 TreeNode nodes = new TreeNode(); 29 nodes.Text = item.ChannelName; 30 nodes.Tag = item; 31 allnode.Nodes.Add(nodes); 32 } 33 } 34 TreeNode allnode = new TreeNode(); 35 //将treeview控件添加初始默认列表 36 public void chishi() 37 { 38 allnode.Text = "所有电台"; 39 TreeNode mynode = new TreeNode(); 40 mynode.Text = "我的电台"; 41 this.tvChannel.Nodes.Add(mynode); 42 this.tvChannel.Nodes.Add(allnode); 43 44 } 45 private void tvChannel_AfterSelect(object sender, TreeViewEventArgs e) 46 { 47 TreeNode selectnodes =this.tvChannel.SelectedNode; 48 if (selectnodes.Level==1&&selectnodes.Parent.Text=="所有电台") 49 { 50 ChannelBase cb = (ChannelBase)selectnodes.Tag; 51 if (cb.TVlist!=null) 52 { 53 cb.TVlist.Clear(); 54 } 55 cb.Fetch(); 56 List<TVprogram> porje = cb.TVlist; 57 this.dgvProgList.DataSource = porje; 58 } 59 } 60 } 61 }
多么伟大的创作!多么神奇的软件!走过路过,千万不要错过!