client/src/Iri.Modernisation.Data/Models/Loader.cs
changeset 30 644e3cd48034
child 34 4d9ebc6fbbe8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/src/Iri.Modernisation.Data/Models/Loader.cs	Thu Jan 14 16:39:50 2010 +0100
@@ -0,0 +1,66 @@
+using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+using System.Xml.Linq;
+using System.IO;
+using System.Collections.Generic;
+using System.Threading;
+namespace Iri.Modernisation.Data.Models
+{
+    /// <summary>
+    /// Classe permettant de charger un XML et de convertir son contenu en un type ReturnType désiré
+    /// </summary>
+    /// <typeparam name="ReturnType">Type Désiré</typeparam>
+    public class Loader<ResultType>
+    {
+        /// <summary>
+        /// WebClient qui permet de charger le XML distant
+        /// </summary>
+        private  WebClient xmlClient = new WebClient();
+        private String _path{get;set;}
+        private Func<XDocument, ResultType> ProcessFunction { get; set; }
+      
+       public Loader(Func<XDocument,ResultType> processFunction)
+        {
+            ProcessFunction = processFunction;
+           
+        }
+        public  void LoadAvailableVideoBooks(String path)
+        {
+            _path = path;
+            xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlClient_DownloadStringCompleted);
+            xmlClient.DownloadStringAsync(new Uri(path,UriKind.RelativeOrAbsolute));
+       
+        }
+       
+        void xmlClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
+        {
+            ResultType result = ProcessFunction.Invoke(XDocument.Parse(e.Result));
+            if (LoaderFinished != null)
+            {
+               
+                LoaderFinished(this, new LoaderEventArgs<ResultType>(result));
+            }
+        }
+
+        public event EventHandler<LoaderEventArgs<ResultType>> LoaderFinished;
+      
+       
+    }
+    public class LoaderEventArgs<ResultType> : EventArgs
+    {
+        public ResultType CreatedObject { get; private set; }
+        public LoaderEventArgs(ResultType createdObject)
+        {
+            CreatedObject = createdObject;
+        }
+    }
+    
+}