The framework and key code of the network TV genie, and the network TV genie framework
Effect:
Programming attributes: broadcast time, time period, name, and video path
Channel base class attributes: channel name, channel program list location, program list abstract method: Fetch ()
Compile the channel subclass to inherit the "channel base class" and implement Fetch () [write method declaration only]
Compile the channel factory class method: to create a channel subclass
Step 1. Create several classes:
01: Create a TV class (TvProgram)
This class is mainly responsible for defining program attributes and providing storage locations for content read from the xml file (FullChannnels. xml.
Attribute:
public DateTime PlayTime { get; set; } public string SomeTime { get; set; } public string Name { get; set; } public string Path { get; set; }
02: create a channel base class (ChannelBase) (parent class or superclass), and this class is an abstract class
Attribute:
public string ChannelName{get; set; } public string Path { get; set; } public string Type { get; set; } public List<TvProgram> Tvlist { get; set; }
Method:
Public abstract List <TvProgram> Fetch ();
03: Create A Class A Channel (TypeAChannel)
This class is mainly responsible for parsing XM files (Beijing TV station. xml)
04: Create a Class B channel)
This class is mainly responsible for parsing XML files (Phoenix TV. xml)
05: Create a factory class (Tool class) (ChannelFactory)
This class is mainly responsible for selecting channels.
Conclusion: ChannelBase, TypeAChannel, TypeBChannel, and ChannelFactory control channel information
06: create a management class (ChannelManager)
Create a method LoadAllChannel (): parse the file
This class actually reduces the amount of code in the form. It is equivalent to the tool class of the program.
Step 2. Core code writing ideas:
01. First, write a LoadAllChannel Method for loading all channels in channel management class ChannelManager;
This method reads the FullChannels. xml file and assigns the read content to the ChannelBase object. The key issue is how to create a ChannelBase object. We all know that ChannelBase is an abstract class and cannot be used to create instances. As a result, we can use its subclass to create objects. Because different subclass instantiation requires multiple new times, we independently write a factory class (ChannelFafactory) to create a subclass instance, but this subclass instance is stored in the parent class variable. After assigning values, we need to add the channel object to the dictionary. After the LoadAllChannel method is completed, the dictionary contains
The data in the XML document. That is, we have implemented
The data in the xml file on the local hard disk is read to a dictionary set in the content.
Code:
Public Dictionary <string, ChannelBase> Fulllist {get {return fulllist ;}set {fulllist = value ;}} private string channelPath = "files/FullChannel. xml "; // new public ChannelManager () {fulllist = new Dictionary <string, ChannelBase> () ;}// parse the xml file public void ReadFiles () {fulllist. clear (); XmlDocument doc = new XmlDocument (); doc. load (channelPath); XmlNode root = doc. documentElement; foreach (XmlNode item in root. childNodes) {ChannelBase channelSB = ChannelFactory. createFile (item ["channelType"]. innerText); channelSB. type = item ["channelType"]. innerText; channelSB. channelName = item ["tvChannel"]. innerText; channelSB. path = item ["Path"]. innerText; fulllist. add (channelSB. channelName, channelSB );}
02: bind data in the set to ListView
The data in the dictionary set is disassembled through Foreach. Each loop creates a TreeNode object.
And assign the attributes in the set to the corresponding attributes of the node. Then add the node at the splicing to the TreeView.
. Note how to find the node [all TV stations] In the TreeView control, because
We need to add a channel name on this node.
The core code is as follows:
ChannelManager manager = new ChannelManager (); manager. readFiles (); Dictionary <string, ChannelBase> dsc = manager. fulllist; foreach (ChannelBase item in dsc. values) {TreeNode td = new TreeNode (); td. text = item. channelName; // channel object td. tag = item; tvlist. nodes [1]. nodes. add (td );}
03: Class A Channel (TypeAChannel)
public override List<TvProgram> Fetch() { XmlDocument doc = new XmlDocument(); doc.Load(Path); XmlNode root = doc.DocumentElement; if (Tvlist == null) { Tvlist = new List<TvProgram>(); } foreach (XmlNode child in root.ChildNodes) { TvProgram tvp = new TvProgram(); if (child.Name == "tvProgramTable") { foreach (XmlNode two in child.ChildNodes) { tvp.PlayTime = Convert.ToDateTime(two["PlayTime"].InnerText); tvp.SomeTime = two["meridien"].InnerText; tvp.Name = two["programName"].InnerText; tvp.Path = two["path"].InnerText; Tvlist.Add(tvp); } } } return this.Tvlist;
04: The method for parsing Class B channels is the same as that for Class A channels.
05: select a channel:
Factory (tool class) (ChannelFactory)
Code:
public static ChannelBase CreateFile(string type) { ChannelBase cb = null; switch (type) { case "TypeA": cb = new TypeAChannel(); break; case "TypeB": cb = new TypeBChannel(); break; default: break; } return cb; }
07: bind data to the dataGriv and write it in the Afterselect event
Code:
TreeNode selectNode = tvlist. selectedNode; if (selectNode. level = 1 & selectNode. parent. text = "all TV stations") {ChannelBase channel = (ChannelBase) selectNode. tag; if (channel. tvlist! = Null) {channel. tvlist. clear () ;}// Method 1: // List <TvProgram> tvlist1 = new List <TvProgram> (); // dgvlist. dataSource = tvlist1; // Method 2: dgvlist. dataSource = channel. fetch ();}
08: add or delete a radio station
Add:
TreeNode selectNOde = tvlist.SelectedNode; ChannelBase channel = (ChannelBase)selectNOde.Tag; tn.Text = channel.ChannelName; tn.Tag = channel; my.Nodes.Add(tn);
Delete:
My. Nodes. Remove (tn );
09.
001:
When there is no channel on my console, the function is not deleted, and cannot be added.
If you have added channels, you can delete them. However, this function cannot be added.
002:
When on all radio stations, there is no delete function, only add function.
The Code is as follows:
TreeNode select = tvlist. SelectedNode; if (select! = Null & select. level = 1) {if (select. parent. text = "my TV station") {tvment. items [0]. visible = false; tvment. items [1]. visible = true;} else {tvment. items [0]. visible = true; tvment. items [1]. visible = false ;}} else {tvment. items [0]. visible = false; tvment. items [1]. visible = false ;}