src/FingersDance.Control.ListVideo/UserControlListVideo.xaml.cs
changeset 95 07972aa3f2e0
parent 84 067f4a6c0cca
child 117 c1222ecbf1b1
--- a/src/FingersDance.Control.ListVideo/UserControlListVideo.xaml.cs	Wed Sep 23 17:11:43 2009 +0200
+++ b/src/FingersDance.Control.ListVideo/UserControlListVideo.xaml.cs	Wed Sep 23 22:22:52 2009 +0200
@@ -12,6 +12,9 @@
 using Microsoft.Surface.Presentation.Controls;
 using System.Collections.Generic;
 using System.Windows.Media.Imaging;
+using System.Xml.Serialization;
+
+using System.Reflection;
 
 namespace FingersDance.Control.ListVideo
 {
@@ -21,32 +24,33 @@
         public event EventHandler EH_ItemVideo1_ContactDown;
         public event EventHandler EH_ItemVideo2_ContactDown;
 
-        List<string> _Videos = new List<string>();
+        public string path = "";
+        static Dictionary<string, ListVideoItem> _Videos = new Dictionary<string, ListVideoItem>();
 
         public UserControlListVideo()
         {
             this.InitializeComponent();
+            LoadList();
+            UpdateList();
 
             // Insert code required on object creation below this point.
         }
 
-        private void ItemVideo1_ContactDown(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
+        //Event appelé lors de la selection d'un Item dans la Video List
+        private void ItemVideo_ContactTapGesture(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
         {
+            path = ((CustomListBoxItem)sender).Path;
             if (EH_ItemVideo1_ContactDown != null)
                 EH_ItemVideo1_ContactDown(this, new EventArgs());
+            
         }
 
-        private void ItemVideo2_ContactDown(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
+        private void ItemVideo_Click(object sender, System.Windows.RoutedEventArgs e)
         {
-            if (EH_ItemVideo2_ContactDown != null)
-                EH_ItemVideo2_ContactDown(this, new EventArgs());
-        }
-
-        //Event appelé lors de la selection d'un Item dans la Video List
-        private void ItemVideo1_ContactTapGesture(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
-        {
+            path = ((CustomListBoxItem)sender).Path;
             if (EH_ItemVideo1_ContactDown != null)
                 EH_ItemVideo1_ContactDown(this, new EventArgs());
+
         }
 
         private void ButtonImporter_ContactDown(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
@@ -70,34 +74,29 @@
                     if (System.IO.File.Exists(PathImporter.Text))
                         Files[0] = PathImporter.Text;
                 if (Files != null)
+                {
                     foreach (string elt in Files)
                         if (elt.ToLower().EndsWith(".wmv")) // .....
                             try
                             {
+                                ListVideoItem newItem = new ListVideoItem();
                                 Image item = getFirstPreview(elt);
-                                if (item != null)
+                                if (item != null && !_Videos.ContainsKey(elt))
                                 {
-                                    SurfaceListBoxItem Contener = new SurfaceListBoxItem();
-                                    Contener.Content = item;
-                                    Contener.ContactHoldGesture += new ContactEventHandler(Contener_ContactHoldGesture);
-                                    stackPanel.Children.Add(Contener);
-
-                                    _Videos.Add(elt);
+                                    newItem.Name = elt.Split('\\')[elt.Split('\\').Length - 1].Split('.')[0];
+                                    newItem.Path = elt;
+                                    newItem.Preview = elt.Replace(".wmv", ".jpg");
+                                    _Videos.Add(newItem.Path, newItem);
                                 }
                             }
                             catch (Exception ex) { }
+                    UpdateList();
+                    SaveList();
+                }
             }
             catch (Exception) { }
         }
 
-        void Contener_ContactHoldGesture(object sender, ContactEventArgs e)
-        {
-            SurfaceContextMenu Menu = new SurfaceContextMenu();
-            Menu.Items.Add("Supprimer");
-            ((SurfaceListBoxItem)sender).ContextMenu = Menu;
-
-        }
-
         Image getFirstPreview(string path)
         {
             MediaPlayer _player = new MediaPlayer();
@@ -120,5 +119,88 @@
 
             return _prev;
         }
+
+        private void RefreshButton_ContactDown(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
+        {
+            LoadList();
+            UpdateList();
+        }
+
+        private void RefreshButton_Click(object sender, System.Windows.RoutedEventArgs e)
+        {
+            LoadList();
+            UpdateList();
+        }
+
+        void LoadList()
+        {
+            _Videos.Clear();
+            StreamReader reader = null;
+            FileInfo assemblyPath = new FileInfo(Assembly.GetExecutingAssembly().Location);
+            DirectoryInfo info = assemblyPath.Directory;
+            try
+            {
+                reader = new StreamReader(info.FullName.ToString() + "\\Resources\\videos.xml");
+                XmlSerializer serializer = new XmlSerializer(typeof(List<ListVideoItem>));
+                List<ListVideoItem> temp = (List<ListVideoItem>)serializer.Deserialize(reader);
+                foreach (ListVideoItem elt in temp)
+                    if (!elt.Preview.Equals("") && !_Videos.ContainsKey(elt.Path))
+                        _Videos.Add(elt.Path, elt);
+                reader.Close();
+            }
+            catch (Exception e)
+            {
+                if (reader != null)
+                    reader.Close();
+            }
+        }
+
+        void SaveList()
+        {
+            StreamWriter writter = null;
+            FileInfo assemblyPath = new FileInfo(Assembly.GetExecutingAssembly().Location);
+            DirectoryInfo info = assemblyPath.Directory;
+            try
+            {
+                writter = new StreamWriter(info.FullName.ToString() + "\\Resources\\videos.xml");
+                XmlSerializer serializer = new XmlSerializer(typeof(List<ListVideoItem>));
+                List<ListVideoItem> temp = new List<ListVideoItem>();
+                foreach (KeyValuePair<string, ListVideoItem> elt in _Videos)
+                    temp.Add(elt.Value);
+                serializer.Serialize(writter, temp);
+                writter.Close();
+            }
+            catch (Exception e)
+            {
+                if (writter != null)
+                    writter.Close();
+            }
+        }
+
+        void UpdateList()
+        {
+            stackPanel.Children.Clear();
+            foreach (KeyValuePair<string, ListVideoItem> elt in _Videos)
+            {
+                try
+                {
+                    if (!elt.Value.Preview.Equals(""))
+                    {
+                        MediaElement item = new MediaElement();
+                        item.Source = new Uri(elt.Value.Preview);
+                        item.Width = 100;
+                        item.Height = 100;
+                        CustomListBoxItem Contener = new CustomListBoxItem();
+                        Contener.Content = item;
+                        Contener.Path = elt.Value.Path;
+                        Contener.Name = elt.Value.Name;
+                        Contener.ContactTapGesture += ItemVideo_ContactTapGesture;
+                        stackPanel.Children.Add(Contener);
+                    }
+                }
+                catch (Exception e)
+                { }
+            }
+        }
     }
 }
\ No newline at end of file