嗨,我是wp8的新手,我正在尝试开发一个应用程序。我有一些问题要从我的 字典我不知道这里的错误是我的代码
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;
}
mu sharedinformation class
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)
您无法在Clear()
上致电List<T>
- 您需要每次都换一个新的:{/ p>
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>
是引用类型(类),因此每次添加时,它都不会添加列表的整个副本 - 它只是将引用添加到同一列表中。下次循环时,清除旧列表并向其添加新项目等。
因此,您将看到的行为是,每个键都会包含 last 标签项目中的项目,因为它们都是完全相同的List<T>
实例。