//按分类生成Grid控件组
        private void CreateGrid(String type)
        {
            //当类型为视频时,动态生成用于显示的Grid
            if (type == "MP4")
            {
                for (int i = 0; i < maxLength; i++)
                {
                    Grid grid = new Grid();
                    grid.Name = "VideoGrid" + videoImage[i].Substring(0, videoImage[i].Length - 4);
                    //在主窗体中注册Grid
                    MainGrid.RegisterName(grid.Name, grid);
                    grid.Width = 130;
                    grid.Height = 120;
                    //设置Grid在主窗体中的位置
                    grid.HorizontalAlignment = HorizontalAlignment.Left;
                    grid.VerticalAlignment = VerticalAlignment.Top;
                    //设置Grid与主窗体的位移
                    int left = 73 + 129 * i;
                    int top = 683;
                    Thickness tk = new Thickness(left, top, 0, 0);
                    grid.Margin = tk;
                    //动态生成缩略图按钮
                    MyButton button = new MyButton();
                    //自定义属性 IsDefaule  false:取消选中  true:选中
                    button.IsDefalut = false;
                    //自定义属性 IsSave  false:没有保存到XML文件中 true:已经保存到XML文件中
                    button.IsSaved = false;
                    button.Name = "Vide" + videoImage[i].Substring(0, videoImage[i].Length - 4);
                    //grid.RegisterName(fi[i].Name.Substring(0, fi[i].Name.Length - 4), button);
                    button.Content = "";
                    button.HorizontalAlignment = HorizontalAlignment.Center;
                    button.VerticalAlignment = VerticalAlignment.Top;
                    //button.HorizontalContentAlignment = HorizontalAlignment.Right;
                    //button.VerticalContentAlignment = VerticalAlignment.Top;
                    button.Width = 130;
                    button.Height = 86;
                    //int left = 70 + 112 * i;
                    //Thickness tk = new Thickness(left, 462, 0, 0);
                    //button.Margin = tk;
                    //自定义按钮样式 ButtonStyle
                    button.Style = Resources["ButtonStyle"] as Style;

                    //设置背景缩略图图片
                    Uri uri = new Uri(ThumbnailPath + "\\" + videoImage[i], UriKind.Relative);
                    BitmapImage bi = new BitmapImage();
                    bi.BeginInit();
                    bi.CacheOption = BitmapCacheOption.OnLoad; //增加这一行
                    bi.UriSource = uri;
                    bi.EndInit();

                    BitmapImage image = new BitmapImage();
                    image = bi;
                    button.Background = new ImageBrush(image);

                    //动态加载按钮的单击事件
                    button.Click += new RoutedEventHandler(Button_Click);
                    //动态加载按钮的双击事件
                    button.MouseDoubleClick += new MouseButtonEventHandler(MouseLeftButton_DoubleClick);
                    //把生成的按钮加载到按钮数组
                    videoImageDisplay[i] = button;

                    //MainGrid.Children.Add(grid);
                    //在Grid里加载Button
                    grid.Children.Add(button);
                    //动态生成缩略图的名称框
                    TextBlock tb = new TextBlock();
                    tb.Text = button.Name.Substring(4, button.Name.Length - 4) + ".MP4";
                    tb.FontSize = 11;
                    tb.Width = 130;
                    tb.Height = 36;
                    tb.TextWrapping = TextWrapping.Wrap;
                    tb.HorizontalAlignment = HorizontalAlignment.Center;
                    tb.VerticalAlignment = VerticalAlignment.Bottom;
                    //tb.HorizontalContentAlignment = HorizontalAlignment.Center;
                    //tb.VerticalContentAlignment = VerticalAlignment.Center;
                    tb.Background = null;
                    //tb.BorderBrush = null;
                    //在Grid中加载名称框
                    grid.Children.Add(tb);
                    //把生成的Grid加载到视频Grid数组
                    videoGrid[i] = grid;
                }
            }
            else if (type == "WAV")
            {
                for (int i = 0; i < soundLength; i++)
                {
                    Grid grid = new Grid();
                    grid.Name = "SoundGrid" + sounds[i].Substring(0, sounds[i].Length - 4);
                    MainGrid.RegisterName(grid.Name, grid);
                    grid.Width = 130;
                    grid.Height = 120;
                    grid.HorizontalAlignment = HorizontalAlignment.Left;
                    grid.VerticalAlignment = VerticalAlignment.Top;
                    int left = 73 + 129 * i;
                    int top = 683;
                    Thickness tk = new Thickness(left, top, 0, 0);

                    grid.Margin = tk;

                    MyButton button = new MyButton();
                    button.IsDefalut = false;
                    button.IsSaved = false;
                    button.Name = "Soun" + sounds[i].Substring(0, sounds[i].Length - 4);
                    //grid.RegisterName(fi[i].Name.Substring(0, fi[i].Name.Length - 4), button);
                    button.Content = "";
                    button.HorizontalAlignment = HorizontalAlignment.Center;
                    button.VerticalAlignment = VerticalAlignment.Top;
                    //button.HorizontalContentAlignment = HorizontalAlignment.Right;
                    //button.VerticalContentAlignment = VerticalAlignment.Top;
                    button.Width = 130;
                    button.Height = 86;
                    //int left = 70 + 112 * i;
                    //Thickness tk = new Thickness(left, 462, 0, 0);
                    //button.Margin = tk;
                    button.Style = Resources["ButtonStyle"] as Style;

                    Uri uri = new Uri(@".\\Resources\\radio.png", UriKind.Relative);
                    BitmapImage image = new BitmapImage(uri);
                    button.Background = new ImageBrush(image);
                    button.Click += new RoutedEventHandler(Button_Click);
                    button.MouseDoubleClick += new MouseButtonEventHandler(MouseLeftButton_DoubleClick);
                    soundImageDisplay[i] = button;

                    //MainGrid.Children.Add(grid);
                    grid.Children.Add(button);

                    TextBlock tb = new TextBlock();
                    tb.Text = button.Name.Substring(4, button.Name.Length - 4) + ".WAV";
                    tb.FontSize = 11;
                    tb.Width = 130;
                    tb.Height = 36;
                    tb.TextWrapping = TextWrapping.Wrap;
                    tb.HorizontalAlignment = HorizontalAlignment.Center;
                    tb.VerticalAlignment = VerticalAlignment.Bottom;
                    //tb.HorizontalContentAlignment = HorizontalAlignment.Center;
                    //tb.VerticalContentAlignment = VerticalAlignment.Center;
                    tb.Background = null;
                    //tb.BorderBrush = null;
                    grid.Children.Add(tb);

                    soundGrid[i] = grid;
                }
            }
            else if (type == "JPG")
            {
                for (int i = 0; i < pictureLength; i++)
                {
                    Grid grid = new Grid();
                    grid.Name = "PictuGrid" + pictures[i].Substring(0, pictures[i].Length - 4);
                    MainGrid.RegisterName(grid.Name, grid);
                    grid.Width = 130;
                    grid.Height = 120;
                    grid.HorizontalAlignment = HorizontalAlignment.Left;
                    grid.VerticalAlignment = VerticalAlignment.Top;
                    int left = 73 + 129 * i;
                    int top = 683;
                    Thickness tk = new Thickness(left, top, 0, 0);
                    grid.Margin = tk;

                    MyButton button = new MyButton();
                    button.IsDefalut = false;
                    button.IsSaved = false;
                    button.Name = "Pict" + pictures[i].Substring(0, pictures[i].Length - 4);
                    //grid.RegisterName(fi[i].Name.Substring(0, fi[i].Name.Length - 4), button);
                    button.Content = "";
                    button.HorizontalAlignment = HorizontalAlignment.Center;
                    button.VerticalAlignment = VerticalAlignment.Top;
                    //button.HorizontalContentAlignment = HorizontalAlignment.Right;
                    //button.VerticalContentAlignment = VerticalAlignment.Top;
                    button.Width = 130;
                    button.Height = 86;
                    //int left = 70 + 112 * i;
                    //Thickness tk = new Thickness(left, 462, 0, 0);
                    //button.Margin = tk;
                    button.Style = Resources["ButtonStyle"] as Style;

                    //做不占用资源处理,解决被占用无法删除的问题
                    Uri uri = new Uri(nameDic[pictures[i].Substring(0, pictures[i].Length - 4)] + "\\" + pictures[i], UriKind.Relative);
                    BitmapImage bi = new BitmapImage();
                    bi.BeginInit();
                    bi.CacheOption = BitmapCacheOption.OnLoad; //增加这一行
                    bi.UriSource = uri;
                    bi.EndInit();
                    BitmapImage image = new BitmapImage();
                    image = bi;
                    button.Background = new ImageBrush(image);
                    button.Click += new RoutedEventHandler(Button_Click);
                    button.MouseDoubleClick += new MouseButtonEventHandler(MouseLeftButton_DoubleClick);
                    pictureImageDisplay[i] = button;

                    //MainGrid.Children.Add(grid);
                    grid.Children.Add(button);

                    TextBlock tb = new TextBlock();
                    tb.Text = button.Name.Substring(4, button.Name.Length - 4) + ".JPG";
                    tb.FontSize = 11;
                    tb.Width = 130;
                    tb.Height = 36;
                    tb.TextWrapping = TextWrapping.Wrap;
                    tb.HorizontalAlignment = HorizontalAlignment.Center;
                    tb.VerticalAlignment = VerticalAlignment.Bottom;
                    //tb.HorizontalContentAlignment = HorizontalAlignment.Center;
                    //tb.VerticalContentAlignment = VerticalAlignment.Center;
                    tb.Background = null;
                    //tb.BorderBrush = null;
                    grid.Children.Add(tb);

                    pictureGrid[i] = grid;
                }
            }
        }
        //生成XML方法
        private void CreateXML(string type,string title,MyButton[] buttons)
        {
            int itemNum = 0;
            foreach (MyButton item in buttons)
            {
                if (item.IsDefalut == true && item.IsSaved == false)
                {
                    if (SaveCheck())
                    {
                        MessageBox.Show("请输入上传文件的备注信息", MainControl.MainConst.MESSAGE_BOX_TITLE);
                        return;
                    }
                    itemNum++;
                }
            }
            if (itemNum == 0)
            {
                MessageBox.Show("请选择要分类保存的文件!", MainControl.MainConst.MESSAGE_BOX_TITLE);
            }
            String[] savedItem = new String[itemNum];
            int num = 0;
            foreach (MyButton item in buttons)
            {
                if (item.IsDefalut == true && item.IsSaved == false)
                {
                    item.IsSaved = true;
                    savedItem[num] = item.Name.Substring(4, item.Name.Length - 4);
                    num++;
                }
            }

            DirectoryInfo files = new DirectoryInfo(@".\\");
            foreach (FileInfo item in files.GetFiles())
            {
                if (item.Name == title + ".xml")
                {
                    foreach (string item2 in savedItem)
                    {
                        index++;
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(item.Name);
                        XmlNode root = xmlDoc.SelectSingleNode("Root");//查找<bookstore>
                        XmlElement xe1 = xmlDoc.CreateElement(type);//创建一个<book>节点
                        //DateTime dt = DateTime.Now;
                        //String uploadTime = dt.ToString("yyyyMMddHHmmss");
                        String shootTime = item2.Split('_')[2];
                        String policeNo = title.Split('_')[0];
                        xe1.SetAttribute("ChangedName", companyName + "_" + policeNo + "_" + policeNo + "_" + shootTime + "_" + timeDic[item2].Replace(":", "-") + "_" + index);//设置该节点genre属性

                        XmlElement xesub1 = xmlDoc.CreateElement("Name");
                        xesub1.InnerText = item2;
                        xe1.AppendChild(xesub1);//添加到<book>节点中
                        XmlElement xesub2 = xmlDoc.CreateElement("Class");
                        xesub2.InnerText = ComboBoxClass.Text;
                        xe1.AppendChild(xesub2);
                        XmlElement xesub3 = xmlDoc.CreateElement("Comments");
                        xesub3.InnerText = TextBoxComments.Text.ToString();
                        xe1.AppendChild(xesub3);
                        XmlElement xesub4 = xmlDoc.CreateElement("SavedFlag");
                        xesub4.InnerText = "0";
                        xe1.AppendChild(xesub4);

                        root.AppendChild(xe1);//添加到<bookstore>节点中
                        xmlDoc.Save(item.Name);
                    }
                    foreach (MyButton item3 in buttons)
                    {
                        if (item3.IsSaved == true)
                        {
                            Grid myGrid = (Grid)item3.Parent;
                            Button bt = myGrid.FindName("Image" + item3.Name.Substring(4, item3.Name.Length - 4)) as Button;
                            Uri uriImage = new Uri(@".\\Resources\\u215.png", UriKind.Relative);
                            BitmapImage newImage = new BitmapImage(uriImage);
                            bt.Background = new ImageBrush(newImage);
                        }
                    }
                    return;
                }
            }

            XmlTextWriter writer = new XmlTextWriter(title + ".xml", System.Text.Encoding.UTF8);
            //使用自动缩进便于阅读
            writer.Formatting = Formatting.Indented;
            //XML声明 
            writer.WriteStartDocument();
            //书写根元素 
            writer.WriteStartElement("Root");
            writer.WriteAttributeString("Org", org);
            foreach (string item in savedItem)
            {
                index++;
                //开始一个元素 
                writer.WriteStartElement(type);
                //向先前创建的元素中添加一个属性
                //DateTime dt = DateTime.Now;
                //String uploadTime = dt.ToString("yyyyMMddHHmmss");
                //dt.GetDateTimeFormats();
                String shootTime = item.Split('_')[2];
                String policeNo = title.Split('_')[0];
                writer.WriteAttributeString("ChangedName", companyName + "_" + policeNo + "_" + policeNo + "_" + shootTime + "_" + timeDic[item].Replace(":", "-") + "_" + index);
                //添加子元素
                writer.WriteElementString("Name", item);
                writer.WriteElementString("Class", ComboBoxClass.Text);
                writer.WriteElementString("Comments", TextBoxComments.Text.ToString());
                writer.WriteElementString("SavedFlag", "0");
                //关闭item元素
                writer.WriteEndElement();
            }
            writer.Close();

            foreach (MyButton item in buttons)
            {
                if (item.IsSaved == true)
                {
                    Grid myGrid = (Grid)item.Parent;
                    Button bt = myGrid.FindName("Image" + item.Name.Substring(4, item.Name.Length - 4)) as Button;
                    Uri uriImage = new Uri(@".\\Resources\\u215.png", UriKind.Relative);
                    BitmapImage newImage = new BitmapImage(uriImage);
                    bt.Background = new ImageBrush(newImage);
                }
            }
        }
        //全部选择事件
        private void selectAll_event(MyButton myButton, Boolean selectAllFlag)
        {
            if (!myButton.IsSaved)
            {
                //得到点击的按钮的父控件Grid
                Grid myGrid = (Grid)myButton.Parent;
                //在Grid中查找是否存在状态按钮图片
                Button bt = myGrid.FindName("Image" + myButton.Name.Substring(4, myButton.Name.Length - 4)) as Button;
                // 全部按钮选择时
                if (selectAllFlag)
                {
                    if (!myButton.IsDefalut)
                    {
                        //动态生成状态按钮
                        Button buttonImage = new Button();
                        buttonImage.Name = "Image" + myButton.Name.Substring(4, myButton.Name.Length - 4);
                        buttonImage.Width = 10;
                        buttonImage.Height = 10;
                        buttonImage.HorizontalAlignment = HorizontalAlignment.Right;
                        buttonImage.VerticalAlignment = VerticalAlignment.Top;
                        Thickness tk = new Thickness(0, 5, 5, 0);
                        buttonImage.Margin = tk;
                        Uri uriImage = new Uri(@".\\Resources\\u199.png", UriKind.Relative);
                        BitmapImage newImage = new BitmapImage(uriImage);
                        buttonImage.Background = new ImageBrush(newImage);

                        myGrid.Children.Add(buttonImage);
                        myGrid.RegisterName("Image" + myButton.Name.Substring(4, myButton.Name.Length - 4), buttonImage);
                        //设置缩略图按钮状态为选中状态
                        myButton.IsDefalut = true;
                    }
                }
                else
                {
                    if (bt != null)
                    {
                        string buttonName = "Image" + myButton.Name.Substring(4, myButton.Name.Length - 4);
                        //移除已生成的状态按钮
                        myGrid.Children.Remove(bt);
                        myGrid.UnregisterName(buttonName);
                        //设置缩略图按钮为未选中状态
                        myButton.IsDefalut = false;
                    }
                }
            }
        }
 //搜索XML文件中已保存的文件
 private void SearchSavedItem(MyButton[] buttons)
 {
     //取得系统当前时间
     DateTime dt = DateTime.Now;
     String xmlDate = dt.ToString("yyyyMMdd");
     //生成XML标题
     String title = userName + "_" + xmlDate;
     //搜索根目录下所有文件
     DirectoryInfo files = new DirectoryInfo(@".\\");
     //遍历根目录下所有文件
     foreach (FileInfo item in files.GetFiles())
     {
         //如果存在已生成的XML
         if (item.Name == title + ".xml")
         {
             //加载此XML文件
             XmlDocument xmlDoc = new XmlDocument();
             xmlDoc.Load(item.Name);
             //查找XML目录下“Name”节点
             XmlNodeList list = xmlDoc.GetElementsByTagName("Name");
             //遍历“Name”节点
             foreach (XmlNode item2 in list)
             {
                 //查找以保存的文件
                 for (int i = 0; i < buttons.Length; i++)
                 {
                     if (buttons[i].Name.Substring(4, buttons[i].Name.Length - 4) == item2.InnerText)
                     {
                         //找到以保存文件的父控件
                         Grid myGrid = (Grid)buttons[i].Parent;
                         //在此以保存的文件缩略图按钮的父控件上生成状态按钮
                         Button buttonImage = new Button();
                         buttonImage.Name = "Image" + buttons[i].Name.Substring(4, buttons[i].Name.Length - 4);
                         buttonImage.Width = 10;
                         buttonImage.Height = 10;
                         buttonImage.HorizontalAlignment = HorizontalAlignment.Right;
                         buttonImage.VerticalAlignment = VerticalAlignment.Top;
                         Thickness tk = new Thickness(0, 5, 5, 0);
                         buttonImage.Margin = tk;
                         Uri uriImage = new Uri(@".\\Resources\\u215.png", UriKind.Relative);
                         BitmapImage newImage = new BitmapImage(uriImage);
                         buttonImage.Background = new ImageBrush(newImage);
                         myGrid.Children.Add(buttonImage);
                         //在父控件中注册状态按钮
                         myGrid.RegisterName("Image" + buttons[i].Name.Substring(4, buttons[i].Name.Length - 4), buttonImage);
                         //设置缩略图按钮的状态为以保存
                         buttons[i].IsSaved = true;
                     }
                 }
             }
         }
     }
 }