2013-12-09 133 views
1

嗨我新与wp8我试图开发一个应用程序。我有一些问题,从我 字典来获得我不知道什么是错在这里是我的代码wp8字典<字符串,列表<object>>已解决

for (int i = 0; i < SharedInformation.tab.Length; i++) 
{ 
    lsCategorie.Clear(); 
    for (int j = 0; j < SharedInformation.SharedLscitation.Count; j++) 
    { 
     if (SharedInformation.tab[i].Equals(SharedInformation.SharedLscitation[j].categorie.ToString())) 
     { 
      lsCategorie.Add(SharedInformation.SharedLscitation[j]); 
     } 
    } 
    SharedInformation.dic.Add(SharedInformation.tab[i], lsCategorie); 
} 

和我的电话

lsCitation = new List<Citation>(); 
lsCitation = (List<Citation>) SharedInformation.dic["amour"]; 

listbox.DataContext = lsCitation; 

我的完整代码

public partial class MainPage : PhoneApplicationPage 
{ 
    private Popup popup; 
    private BackgroundWorker backroungWorker; 
    ShareStatusTask quotesh = new ShareStatusTask(); 
    SmsComposeTask quotesms = new SmsComposeTask(); 
    List<Citation> lsCitation = new List<Citation>(); 
    List<Citation> lsCategorie = new List<Citation>(); 

    // Constructeur 
    public MainPage() 
    { 
     InitializeComponent(); 
     ShowSplash(); 

    } 

    private void ShowSplash() 
    { 
     this.popup = new Popup(); 
     this.popup.Child = new SplashScreen(); 
     this.popup.IsOpen = true; 
     StartLoadingData(); 
    } 

    private void StartLoadingData() 
    { 
     backroungWorker = new BackgroundWorker(); 
     backroungWorker.DoWork += new DoWorkEventHandler(backroungWorker_DoWork); 
     backroungWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backroungWorker_RunWorkerCompleted); 
     backroungWorker.RunWorkerAsync(); 
    } 

    void backroungWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     this.Dispatcher.BeginInvoke(() => 
     { 
      this.popup.IsOpen = false; 

     } 
     ); 
    } 

    void backroungWorker_DoWork(object sender, DoWorkEventArgs e) 
    { 
     //here we can load data 
     Thread.Sleep(9000); 

     if (IsolatedStorageSettings.ApplicationSettings.Contains("data") == false) 
     { 
      InitializeComponent(); 
      WebClient web = new WebClient(); 
      web.DownloadStringCompleted += web_DownloadStringCompleted; 
      string uri = "http://quotesconsommation.azurewebsites.net/json/json.php"; 
      web.DownloadStringAsync(new Uri(uri)); 
      IsolatedStorageSettings.ApplicationSettings["data"] = 1; 
      IsolatedStorageSettings.ApplicationSettings["citation"] = lsCitation; 
      SharedInformation.SharedLscitation = lsCitation; 
      MessageBox.Show("" + NetworkInterface.GetIsNetworkAvailable() + ""); 

     } 
     else 
     { 
      SharedInformation.SharedLscitation = IsolatedStorageSettings.ApplicationSettings["citation"] as List<Citation>; 
     } 
     for (int i = 0; i < SharedInformation.tab.Length; i++) 
     { 
      lsCategorie.Clear(); 
      for (int j = 0; j < SharedInformation.SharedLscitation.Count; j++) 
      { 

       if (SharedInformation.tab[i].Equals(SharedInformation.SharedLscitation[j].categorie.ToString())) 
       { 
        lsCategorie.Add(SharedInformation.SharedLscitation[j]); 
       } 
      } 
      SharedInformation.dic.Add(SharedInformation.tab[i], lsCategorie); 
     } 
    } 
    void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result); 


     foreach (var blog in rootObject.citation) 
     { 
      lsCitation.Add(blog); 
     } 

,并在那里我拨打电话

List<Citation> lsCitation; 
    public TOUTES() 
    { 
     InitializeComponent(); 


     lsCitation = new List<Citation>(); 
     lsCitation = (List<Citation>) SharedInformation.dic["amour"]; 

     listbox.DataContext = lsCitation; 

    } 

亩sharedinformation类

public static class SharedInformation 
{ 

    public static Citation Sharedcitation; 
    public static List<Citation> SharedLscitation; 
    public static Dictionary<string, List<Citation>> dic = new Dictionary<string, List<Citation>>(); 
    public static String[] tab = { "amour", "art et spectacle", "arts et creation", "bonheur", "cinema", "cultures", "famille", "fetes", "humour", "insolite", "livres et lettres", "musique", "nature", "philosophie", "pratique", "proverbes", "sagesse", "sciences", "sport et loisirs", "theatre", "travail" }; 
    public static bool connectionstatus; 
} 
+0

究竟是什么问题? –

回答

2

你不能在List<T>Clear() - 你需要一个新的每次:

for (int i = 0; i < SharedInformation.tab.Length; i++) 
{ 
    // Make a new list, don't reuse it! 
    lsCategorie = new List<object>(); 
    for (int j = 0; j < SharedInformation.SharedLscitation.Count; j++) 
    { 

由于List<T>是引用类型(类),每次添加时,都不会添加列表的整个副本 - 它只是将引用添加到同一个列表中。通过你的循环下一次,你清楚了列表和添加新的项目给它,等

因此,你会看到的行为是每个关键还得从去年标签的项目物品,因为它们都是完全相同的List<T>实例。

+0

仍然无法使用! 我会更基本地解释我从一个天蓝色的数据库中获取我的所有信息我使用json来解析数据并获得静态对象列表中的结果,但是我需要从某些属性中排序这些对象,并将结果在一个词典,使呼叫更容易 – Fuujiin

+0

@ user3084145我试图给你从你的问题的具体情况 - 但有太多的问题,可能有问题。您需要在调试器下运行它,并在从Azure中获取值时检查值以查看发生了什么。 –

+0

我从asure获得所有数据,但它工作正常,但行 – Fuujiin

相关问题