2013-12-09 124 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

相關問題