Partage
  • Partager sur Facebook
  • Partager sur Twitter

[WP7]Problème avec IsolatedStorageSettings

    13 mai 2012 à 16:32:29

    Bonjour mes amis les zéros, je viens vous soumettre un petit problème
    Je voudrai utiliser IsolatedStorageSettings pour stocker une liste d'objet d'une classe, mais quand je récupère cela depuis IsolatedStorageSettings je n'ai rien qu'une liste vide
    Je laisse l'émulateur ouvert comme précisé dans le tutoriel mais rien n'y fait

    voici le code de mes classes :
    Site.cs
    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;
    
    namespace FluxRSS
    {
        public class Site
        {
            public string url {get; set;}
    
            public bool abonnement { get; set; }
    
            public Site(string u) 
            {
                this.url = u;
                this.abonnement = true;
            }
        }
    }
    


    mainpage.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.IO.IsolatedStorage;
    using System.ServiceModel.Syndication;
    using System.IO;
    using System.Xml;
    
    namespace FluxRSS
    {
        public partial class MainPage : PhoneApplicationPage
        {
    
            //WebClient pour la connexion avec le flux
            private WebClient client;
    
            //Liste des flux rss deja enregistre
            private List<Site> listeSite;
    
            //Compteur pour la liste 
            private int nbTotal;
    
            //i liste
            private int iListe;
    
            //liste des flux
            private List<SyndicationFeed> listeFlux;
    
                
            // Constructeur
            public MainPage()
            {
                InitializeComponent();
    
                //on charge la liste des sites si elle exite déja 
                this.loadListeSite();
    
                //initialisation des compteur
                nbTotal = listeSite.Count;
                iListe = 0;
    
                //initialisation de la liste des flux
                listeFlux = new List<SyndicationFeed>();
    
                //on telecharge un par un les flus rss
                client = new WebClient();
                client.DownloadStringCompleted += client_DownloadStringCompleted;
                
                
    
            }
    
            private void loadListeSite()
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains("ListeFlux"))
                {
                    listeSite = (List<Site>)IsolatedStorageSettings.ApplicationSettings["ListeFlux"];
                }
                else
                {
                    listeSite = new List<Site>();
                    
                }
                //IsolatedStorageSettings.ApplicationSettings["ListeFlux"] = listeSite;
            }
    
            private void AjouterFlux_Click(object sender, RoutedEventArgs e)
            {
                NavigationService.Navigate(new Uri("/AddFlux.xaml", UriKind.Relative));
            }
    
            protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
            {
                
                this.loadListeSite();
                IsolatedStorageSettings.ApplicationSettings["ListeFlux"] = listeSite;
                IsolatedStorageSettings.ApplicationSettings.Save();
                if (listeSite.Count == 0)
                {
                    NavigationService.Navigate(new Uri("/AddFlux.xaml", UriKind.Relative));
                }
                else
                {
                    //initialisation des compteur
                    nbTotal = listeSite.Count;
                    iListe = 0;
                    client.DownloadStringAsync(new Uri(listeSite[0].url));
                }
               
                base.OnNavigatedTo(e);
            }
    
            private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {
                if (e.Error == null)
                {
                    this.ajouteFlux(e.Result);
                    if (iListe < nbTotal - 2)
                    {
                        iListe++;
                        
                        client.DownloadStringAsync(new Uri(listeSite[iListe].url));
                    }
                }
            }
    
            private void ajouteFlux(string flux)
            {
                StringReader sr = new StringReader(flux);
                XmlReader xml = XmlReader.Create(sr);
                SyndicationFeed feed = SyndicationFeed.Load(xml);
                listeFlux.Add(feed);
            }
    
            
        }
    }
    


    AddFlux.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    using System.IO.IsolatedStorage;
    
    namespace FluxRSS
    {
    
        public partial class AddFlux : PhoneApplicationPage
        {
    
            
    
            private ObservableCollection<Site> liste;
            public ObservableCollection<Site> Liste
            {
                get
                {
                    return liste;
                }
                set
                {
                    if (value == liste)
                        return;
                    liste = value;
                    NotifyPropertyChanged("Liste");
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(string p)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
    
            public AddFlux()
            {
                InitializeComponent();
                List<Site> l = (List<Site>)IsolatedStorageSettings.ApplicationSettings["ListeFlux"];
                Liste = new ObservableCollection<Site>(l);
               /* Liste = new ObservableCollection<Site>();
                Liste.Add(new Site("http://feeds2.feedburner.com/blogeee/articles"));
                Liste.Add(new Site("http://feeds2.feedburner.com/KorbensBlog-UpgradeYourMind"));
                Liste.Add(new Site("http://feeds.feedburner.com/LeJournalduGamer"));
                Liste.Add(new Site("http://feeds2.feedburner.com/LeJournalduGeek"));
                Liste.Add(new Site("http://www.siteduzero.com/Templates/xml/news_fr.xml"));
                Liste.Add(new Site("http://www.cowcotland.com/backend.php"));*/
                
    
                DataContext = this;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Uri url ;
                if (Uri.TryCreate(flux.Text, UriKind.RelativeOrAbsolute, out url))
                {
                    Liste.Add(new Site(flux.Text));
                    flux.Text = "";
                    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    
                }
                else
                {
                    MessageBox.Show("L'url que vous avez entré est incorrecte !");
                }
            }
    
            protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
            {
                List<Site> l = new List<Site>();
                foreach (Site s in Liste)
                {
                    l.Add(new Site(s.url));
                }
                
                IsolatedStorageSettings.ApplicationSettings["ListeFlux"] = l;
                IsolatedStorageSettings.ApplicationSettings.Save();
                base.OnNavigatedFrom(e);
            }
        }
    }
    


    Merci de votre aide
    • Partager sur Facebook
    • Partager sur Twitter
      14 mai 2012 à 23:04:45

      Ce ne sont pas les IsolatedStorageSettings que tu dois utiliser, mais un fichier dans l'Isolated Storage. Tu peux créer ce fichier en sérialisant tes données comme ceci :

      private void saveListeSite()
      {
          string fileName = "flux.xml";
          using(IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
          {
              using(IsolatedStorageFileStream stream = storage.CreateFile(fileName))
              {
                  XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Site>));
                  xmlSerializer.Serialize(stream, listeSite);
              }
          }
      }
      

      Et le chargement se fait comme ceci :

      private void loadListeSite()
      {
          string fileName = "flux.xml";
          using(IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
          {
              if(!storage.FileExists(fileName))
              {
                  listeSite = new List<Site>();
                  return;
              }
      
              using(IsolatedStorageFileStream stream = storage.OpenFile(fileName, FileMode.Open))
              {
                  XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Site>));
                  listeSite = xmlSerializer.Deserialize(stream) as List<Site>;
              }
          }
      }
      

      N'oublie pas d'ajouter une référence à System.Xml.Serialization à ton projet. :)
      • Partager sur Facebook
      • Partager sur Twitter

      [WP7]Problème avec IsolatedStorageSettings

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown