(深入.Net平台和C#編程)第八章.上機練習(網絡電視精靈).20170415


==============================================XML文件==============================================

-----------------------------------------電視台XML-----------------------------------------

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <TVChannels>
 3   <Channel>
 4     <channelType>TypeA</channelType>
 5     <tvChannel>北京電視台</tvChannel>
 6     <path>TypeA.xml</path>
 7   </Channel>
 8 
 9   <Channel>
10     <channelType>TypeB</channelType>
11     <tvChannel>廣州電視台</tvChannel>
12     <path>TypeB.xml</path>
13   </Channel>
14 </TVChannels>
TV.xml

-----------------------------------------北京電視台XML-----------------------------------------

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <!--頻道A-->
 3 <typeA>
 4   <!--電視台名稱-->
 5   <channelName>北京電視台</channelName>
 6   <!--電視台節目清單-->
 7   <tvProgramTable>
 8     <Program>
 9       <playTime>2017-04-15 6:02</playTime>
10       <!--播出時間-->
11       <ProgramName>《還珠格格》</ProgramName>
12       <!--節目名稱-->
13       <ProgramPath>d:/video/《還珠格格》.mp4</ProgramPath>
14       <!--節目路徑-->
15     </Program>
16 
17     <Program>
18       <playTime>2017-04-16 7:00</playTime>
19       <!--播出時間-->
20       <ProgramName>《情深深雨蒙蒙》</ProgramName>
21       <!--節目名稱-->
22       <ProgramPath>d:/video/《情深深雨蒙蒙》.mp4</ProgramPath>
23       <!--節目路徑-->
24     </Program>
25 
26     <Program>
27       <playTime>2017-04-17 8:08</playTime>
28       <!--播出時間-->
29       <ProgramName>《西游記》</ProgramName>
30       <!--節目名稱-->
31       <ProgramPath>d:/video/《西游記》.mp4</ProgramPath>
32       <!--節目路徑-->
33     </Program>
34    </tvProgramTable>
35 </typeA>
TypeA.xml

-----------------------------------------廣州電視台XML-----------------------------------------

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <!--頻道B-->
 3 <typeB>
 4   <!--電視台名稱-->
 5   <channelName>廣州電視台</channelName>
 6   <!--電視台節目清單-->
 7   <tvProgramTable>
 8       <program>
 9         <playTime>2017-04-15 8:00</playTime>
10         <!--播出時間-->
11         <ProgramName>《手撕鬼子》</ProgramName>
12         <!--節目名稱-->
13         <ProgramPath>d:/video/《手撕鬼子》.mp4</ProgramPath>
14         <!--節目路徑-->
15       </program>
16 
17       <program>
18         <playTime>2017-04-16 9:09</playTime>
19         <!--播出時間-->
20         <ProgramName>《石頭砸飛機》</ProgramName>
21         <!--節目名稱-->
22         <ProgramPath>d:/video/《石頭砸飛機》.mp4</ProgramPath>
23         <!--節目路徑-->
24       </program>
25 
26       <program>
27         <playTime>2017-04-17 10:10</playTime>
28         <!--播出時間-->
29         <ProgramName>《水滸傳》</ProgramName>
30         <!--節目名稱-->
31         <ProgramPath>d:/video/《水滸傳》.mp4</ProgramPath>
32         <!--節目路徑-->
33       </program>
34     </tvProgramTable>
35 </typeB>
TypeB.xml

 

==============================================entity==============================================

-----------------------------------------電視節目屬性類-----------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace SJ5.entity
 8 {
 9     /// <summary>
10     /// 電視節目屬性類
11     /// </summary>
12     public class TvProgram
13     {
14         /// <summary>
15         /// 播出時間
16         /// </summary>
17         public DateTime PlayTime { get; set; }
18         /// <summary>
19         /// 節目名稱
20         /// </summary>
21         public string ProgramName { get; set; }
22         /// <summary>
23         /// 節目路徑
24         /// </summary>
25         public string ProgramPath { get; set; }
26     }
27 }
TvProgram

-----------------------------------------父類電視節目類-----------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace SJ5.entity
 8 {  
 9     /// <summary>
10     /// 電視節目類
11     /// </summary>
12     public abstract class ChannelBase
13     {
14         /// <summary>
15         /// 頻道名稱
16         /// </summary>
17         public string ChannelName { get; set; }
18         /// <summary>
19         /// Xml文件路徑
20         /// </summary>
21         public string Path { get; set; }
22         /// <summary>
23         /// 節目列表
24         /// </summary>
25         public List<TvProgram> ProgramList { get; set; }
26 
27         public ChannelBase()
28         {
29             this.ProgramList = new List<TvProgram>();
30         }
31         /// <summary>
32         /// 解析頻道節目單信息
33         /// </summary>
34         public abstract void Fetch();
35     }
36 }
ChannelBase

-----------------------------------------子類頻道A(北京電視台)類-----------------------------------------

 1 using SJ5.entity;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Xml;
 8 
 9 namespace SJ5.entity
10 {
11     /// <summary>
12     /// 頻道A
13     /// </summary>
14     public class TypeAChannel:ChannelBase
15     {
16 
17         public override void Fetch()
18         {
19             XmlDocument xmlDoc = new XmlDocument();
20             xmlDoc.Load(base.Path);
21             //XmlElement elem = xmlDoc.DocumentElement;
22             XmlNode xmlRoot = xmlDoc.DocumentElement;
23             ProgramList = new List<TvProgram>();
24             foreach (XmlNode node in xmlRoot.ChildNodes)
25             {
26                 if (node.Name == "tvProgramTable")
27                 {
28                     foreach (XmlNode node1 in node.ChildNodes)
29                     {
30                         //新建節目類型對象
31                         TvProgram program = new TvProgram();
32                         //根據xml數據賦值對應屬性
33                         program.PlayTime = DateTime.Parse(node1["playTime"].InnerText);
34                         program.ProgramName = node1["ProgramName"].InnerText;
35                         program.ProgramPath = node1["ProgramPath"].InnerText;
36                         //將節目添加到節目列表
37                         ProgramList.Add(program);
38                     }
39                 }
40             }
41         }
42     }
43 }
TypeAChannel

-----------------------------------------父類頻道B(廣州電視台)類-----------------------------------------

 1 using SJ5.entity;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Xml;
 8 
 9 namespace SJ5.entity
10 {
11     /// <summary>
12     /// 頻道B
13     /// </summary>
14     public class TypeBChannel : ChannelBase
15     {
16         public override void Fetch()
17         {
18             XmlDocument xmlDoc = new XmlDocument();
19             xmlDoc.Load(base.Path);
20             //XmlElement elem = xmlDoc.DocumentElement;
21             XmlNode xmlRoot = xmlDoc.DocumentElement;
22             ProgramList = new List<TvProgram>();
23           
24             foreach (XmlNode node in xmlRoot.ChildNodes)
25             {
26                 if (node.Name == "tvProgramTable")
27                 {
28                     foreach (XmlNode node1 in node.ChildNodes)
29                     {
30                         //新建節目類型對象
31                         TvProgram program = new TvProgram();
32                         //根據xml數據賦值對應屬性
33                         program.PlayTime = DateTime.Parse(node1["playTime"].InnerText);
34                         program.ProgramName = node1["ProgramName"].InnerText;
35                         program.ProgramPath = node1["ProgramPath"].InnerText;
36                         //將節目添加到節目列表
37                         ProgramList.Add(program);
38                     }
39                 }
40             }
41         }
42     }
43    
44 }
TypeBChannel

 

=======================節目管理類=======================

 1 using SJ5.entity;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Xml;
 8 
 9 namespace SJ5.entity
10 {
11     /// <summary>
12     /// 節目管理類
13     /// </summary>
14     public class ChannelManager
15     {
16         //XML文件路徑
17         public string channelPath = "TV.xml";
18         //所有頻道集合
19         public Dictionary<string, ChannelBase> channelBase = new Dictionary<string, ChannelBase>();
20 
21         /// <summary>
22         /// 根據頻道類型創建頻道
23         /// </summary>
24         /// <param name="type"></param>
25         /// <returns></returns>
26         public ChannelBase CreateChannel(string type)
27         {
28             ChannelBase cb = null;
29             if (type == "TypeA")
30             {
31                 cb = new TypeAChannel();
32             }
33             if (type == "TypeB")
34             {
35                 cb = new TypeBChannel();
36             }
37             return cb;
38         }
39 
40         /// <summary>
41         /// 加載節目
42         /// </summary>
43         public void LoadChannel()
44         {
45             XmlDocument xmlDoc = new XmlDocument();
46             xmlDoc.Load(this.channelPath);
47             XmlNode xmlRoot = xmlDoc.DocumentElement;
48             //將所有頻道添加到channelBase泛型集合
49             foreach (XmlNode node in xmlRoot.ChildNodes)
50             {
51                 //根據類型創建不同子類
52                 ChannelBase cb = CreateChannel(node["channelType"].InnerText);
53                 //根據創建不同子類的對象將不同頻道名稱賦值給屬性
54                 cb.ChannelName = node["tvChannel"].InnerText;
55                 //根據創建不同子類的對象把不同頻道的xml地址賦值屬性
56                 cb.Path = node["path"].InnerText;
57                 //根據不同子類對象執行重寫方法 給 節目列表賦值
58                 cb.Fetch();
59                 //將不同頻道 賦值給所有頻道泛型集合
60                 channelBase.Add(cb.ChannelName, cb);
61             }
62         }
63 
64     }
65 }
ChannelManager

 

==============================================窗體==============================================

  1 using SJ5.entity;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.ComponentModel;
  5 using System.Data;
  6 using System.Drawing;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 using System.Windows.Forms;
 11 
 12 namespace SJ5
 13 {
 14     public partial class frmMain : Form
 15     {
 16         public frmMain()
 17         {
 18             InitializeComponent();
 19             //初始把ContextMenuStrip兩個選項隱藏
 20             cmsRight.Items[0].Visible = false;
 21             cmsRight.Items[1].Visible = false;
 22         }
 23 
 24         ChannelManager cm = new ChannelManager();
 25 
 26         /// <summary>
 27         /// 加載事件
 28         /// </summary>
 29         /// <param name="sender"></param>
 30         /// <param name="e"></param>
 31         private void frmMain_Load(object sender, EventArgs e)
 32         {
 33             InitList();
 34             dgvChannelList.AutoGenerateColumns = false;
 35         }
 36 
 37         /// <summary>
 38         /// 初始化TreeView 選項
 39         /// </summary>
 40         public void InitList()
 41         {
 42             tvTV.Nodes.Add("我的電視台");
 43             tvTV.Nodes.Add("所有電視台");
 44             //加載節目
 45             cm.LoadChannel();
 46             //將所有頻道名稱加到所有電視台
 47             foreach (var cb in cm.channelBase)
 48             {
 49                 tvTV.Nodes[1].Nodes.Add(cb.Key);
 50             }
 51             //展開所有節點
 52             tvTV.ExpandAll();
 53         }
 54 
 55         //0刪除 1添加
 56         private void tvTV_MouseClick(object sender, MouseEventArgs e)
 57         {
 58             //如果選中的是我的電視台的子節點則把 刪除 顯示 , 加入 隱藏
 59             if (tvTV.SelectedNode.Parent != null && tvTV.SelectedNode.Parent.Text.Equals("我的電視台"))
 60             {
 61                 cmsRight.Items[1].Visible = false;
 62                 cmsRight.Items[0].Visible = true;
 63             }
 64             //如果選中的是所有電視台的子節點則把 隱藏 顯示 , 刪除 隱藏
 65             else
 66             {
 67                 cmsRight.Items[0].Visible = false;
 68                 cmsRight.Items[1].Visible = true;
 69             }
 70             //如果選中的是根則把 兩個 都隱藏
 71             if (tvTV.SelectedNode.Level == 0)
 72             {
 73                 cmsRight.Items[0].Visible = false;
 74                 cmsRight.Items[1].Visible = false;
 75             }
 76         }
 77 
 78         /// <summary>
 79         /// 右鍵“加入到我的電視台”
 80         /// </summary>
 81         /// <param name="sender"></param>
 82         /// <param name="e"></param>
 83         private void tsmiAddMyTV_Click(object sender, EventArgs e)
 84         {
 85             //循環判斷“我的電視台”里有無重復頻道
 86             foreach (TreeNode tr in tvTV.Nodes[0].Nodes)
 87             {
 88                 if (tr.Text.Equals(tvTV.SelectedNode.Text))
 89                 {
 90                     MessageBox.Show("已有該頻道");
 91                     return;
 92                 }
 93             }
 94             tvTV.Nodes[0].Nodes.Add(tvTV.SelectedNode.Text);
 95             //展開“我的電視台”的節點
 96             tvTV.Nodes[0].Expand();
 97         }
 98 
 99         /// <summary>
100         /// 右鍵刪除
101         /// </summary>
102         /// <param name="sender"></param>
103         /// <param name="e"></param>
104         private void tsmiDelete_Click(object sender, EventArgs e)
105         {
106             tvTV.SelectedNode.Remove();
107         }
108 
109         /// <summary>
110         /// 選擇節點后事件
111         /// </summary>
112         /// <param name="sender"></param>
113         /// <param name="e"></param>
114         private void tvTV_AfterSelect(object sender, TreeViewEventArgs e)
115         {
116             BangChannel();
117         }
118 
119         public void BangChannel()
120         {
121             //如果選中根就不顯示節目
122             if (tvTV.SelectedNode.Level == 0)
123             {
124                 dgvChannelList.DataSource = null;
125                 return;
126             }
127             //用選中的頻道找 節目列表
128             dgvChannelList.DataSource = cm.channelBase[tvTV.SelectedNode.Text].ProgramList;
129         }
130 
131     }
132 }
frmMain

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM