zoukankan      html  css  js  c++  java
  • (深入.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

  • 相关阅读:
    招聘、外包和求职;找人、找活和找工作的都来看看。
    这周我加星(6)
    走出行业暴利思维,开始为“软件”付钱!
    真相,道歉。
    这周我加星(8-11)
    独家:Havok 发布新的 AI 中间件
    一奖三年得,终获 CSDN MVB,与大家分享喜悦
    “解决”OpenCASCADE图形设备初始化问题
    如何在Debian上安装ATI官方驱动
    VC++/MFC学习笔记(六)
  • 原文地址:https://www.cnblogs.com/1-2-3-4/p/6716100.html
Copyright © 2011-2022 走看看