Effect:
Write Program class properties: Broadcast time, period, name, video path
Write Channel base class properties: Channel name, channel program location, program list abstract method:Fetch ()
Write channel subclass Inherits " Channel base class ", implements Fetch ()"write-only method declaration"
Write Channel factory class method: Implement Create channel subclass
First step. Create several classes:
: Create a TV class (Tvprogram)
This class is primarily responsible for defining the properties of the program, providing a place to store subsequent content read from the XML file (fullchannnels.xml).
Properties are:
Public Get Set ; } Public string Get Set ; } Public string Get Set ; } Public string Get set; }
02: Create a Channel base class (Channelbase) (parent class or superclass), and this class is abstract
Properties are:
Public string channelname{getset;} Public string Get Set ; } Public string Get Set ; } Public Get set; }
Method:
Public abstract list<tvprogram> Fetch ();
03: Create a Class A channel (Typeachannel)
This class is primarily responsible for interpreting XM files (Beijing TV. xml)
04: Create a Class B channel (Typebchannel)
This class is primarily responsible for parsing XML files (Phoenix satellite TV . Xml)
: Create a factory Class (Tool Class) (channelfactory)
This class is mainly responsible for the selection of channels
Summary: Channelbase,typeachannel,typebchannel,channelfactory Four classes of main control channel information
: Creation of a management class (ChannelManager)
Create a Method Loadallchannel (): Parse file
The primary role of this class essentially eases the amount of code in the form. Equivalent to a program's tool class.
The second step. The core idea of writing code:
01. First in the channel management class ChannelManager write a load all channel method Loadallchannel;
The method functions to read the fullchannels.xml file and assigns the read content to the channelbase object. The key question turns into how to create a Channelbase object. We all know that channelbase itself is an abstract class and cannot create an instance. From this, we think that we can create objects through its subclasses. Since the instantiation of different subclasses requires multiple new, we have individually written a factory class (Channelfafactory) to create the subclass instance, but the subclass instance is stored in the parent class variable. After the assignment is complete, we need to add the channel object to the dictionary. Then after the Loadallchannel method is finished, the dictionary has
The data in the XML document. That is, we realized the
The data in the XML file on the local hard disk is read into a dictionary collection in the content.
Code:
Publicdictionary<string, channelbase>Fulllist {Get{returnFulllist;} Set{fulllist =value;} } Private stringChannelpath ="Files/fullchannel.xml"; //Constructor in New PublicChannelManager () {fulllist=Newdictionary<string, channelbase>(); } //Parsing XML Files Public voidReadfiles () {fulllist. Clear (); XmlDocument Doc=NewXmlDocument (); Doc. Load (Channelpath); XmlNode Root=Doc. DocumentElement; foreach(XmlNode IteminchRoot. 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 collection to the ListView
Splits the data in the dictionary collection through foreach, creating a TreeNode object for Each loop.
and assigns the attributes in the collection to the corresponding properties of the node. Then add the nodes of the splice to the TreeView
。 It is important to note how to find the "all stations" node in the TreeView control, because
We need to add the channel name on that node
The core code is as follows:
ChannelManager Manager =NewChannelManager (); Manager. Readfiles (); Dictionary<string, channelbase> DSC =Manager. Fulllist; foreach(Channelbase IteminchDSC. Values) {TreeNode TD=NewTreeNode (); Td. Text=item. ChannelName; //Channel ObjectTD. Tag=item; Tvlist. nodes[1]. Nodes.Add (TD); }
03:A type channel (Typeachannel)
Public OverrideList<tvprogram>Fetch () {XmlDocument doc=NewXmlDocument (); Doc. Load (Path); XmlNode Root=Doc. DocumentElement; if(Tvlist = =NULL) {tvlist=NewList<tvprogram>(); } foreach(XmlNode ChildinchRoot. ChildNodes) {Tvprogram TVP=NewTvprogram (); if(Child. Name = ="tvprogramtable") { foreach(XmlNodeinchChild . 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;
The 04:b class channel (Typebchannel) and Class A parsing methods are the same.
to: Select the Channel:
Factory Class (Tool Class) (channelfactory)
Code:
Public StaticChannelbase CreateFile (stringtype) {Channelbase CB=NULL; Switch(type) { Case "TypeA": CB=NewTypeachannel (); Break; Case "TypeB": CB=NewTypebchannel (); Break; default: Break; } returnCB; }
07: Bind data to Datagriv, write in AfterSelect event
Code:
TreeNode Selectnode =tvlist. SelectedNode; if(selectnode.level==1&&selectnode.parent.text=="All Stations") {channelbase channel=(channelbase) Selectnode.tag; if(channel. Tvlist! =NULL) {channel. Tvlist.clear (); } //Way One://list<tvprogram> tvlist1 = new list<tvprogram> (); //dgvlist. DataSource = Tvlist1; //Way Two:dgvlist. DataSource=Channel. Fetch (); }
08: Implement add station and delete station
Add to:
TreeNode Selectnode = tvlist. SelectedNode; = (channelbase) Selectnode.tag; = Channel. ChannelName; = Channel; My. Nodes.Add (TN);
Delete:
My. Nodes.remove (TN);
09.
001:
When there is no channel in my station, there is no delete function, even more cannot add function
If you have a channel that you have added, you can delete the feature. However, there is no added functionality at this time.
002:
When on all stations, there is no delete function, only add function.
The code is as follows:
TreeNodeSelect=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; }
The framework and key code of network TV elves