【问题标题】:Windows Phone 7 Listbox not updatingWindows Phone 7 列表框未更新
【发布时间】:2012-05-25 13:56:40
【问题描述】:

我正在从 Internet 中提取一个 XML 文件并将其加载到手机上。一切正常,减去列表框在应用程序初始加载时未更新。

应用的简单路径如下:

在应用程序加载时,如果它已连接到互联网,它会运行更新联系人方法。如果不是,它会检查它是否是第一次加载,如果是,它会在运行应用程序之前通知用户连接到移动或无线网络,否则它会加载旧数据。

在更新方法中,它会检查网络服务以查看自上次更新以来是否有任何更新。如果它是第一次加载,它说已经有 2000 次更新,强制更新。如果更新次数大于零,它会运行下载然后加载数据(这是这里的问题),否则它会加载旧数据(当前数据)。

问题是下载后,它不会加载数据。这是所述项目的代码。

getData 函数:

private void getData()
        {
            try
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("contacts.xml", FileMode.Open);
                    using (StreamReader reader = new StreamReader(isoFileStream))
                    {
                        XElement xmlContact = XElement.Parse(reader.ReadToEnd());
                        lstContacts.ItemsSource = from contact in xmlContact.Descendants("contact")
                                                               select new ContactItem
                                                               {
                                                                   ImageSource = contact.Element("Image").Value,
                                                                   FName = contact.Element("FName").Value,
                                                                   LName = contact.Element("LName").Value,
                                                                   Extension = contact.Element("Extension").Value,
                                                                   Email = contact.Element("Email").Value,
                                                                   Cell = contact.Element("Cell").Value,
                                                                   Title = contact.Element("TitleName").Value,
                                                                   Dept = contact.Element("deptName").Value,
                                                                   Office = contact.Element("officename").Value,
                                                                   ID = contact.Element("ID").Value
                                                               };
                    }
                }
            }
            catch
            {
            }
        }

更新函数:

void contact_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                return;
            }

            XElement xmlContact = XElement.Parse(e.Result);
            string updates = xmlContact.Element("updates").Element("number").Value;
            if (firstrun)
            {
                updates = "2000";
                settings["firstRun"] = (bool)false;
            }
            MessageBox.Show(updates);
            if (Convert.ToInt32(updates) > 0)
            {
                MessageBox.Show("Updating Contacts");
                ContactReader cr = new ContactReader("http://domain.tld/RLContactApp/getContactsWP7.php?sqlQueryType=C&lastUpdated=01%2F01%2F2000&format=xml", "contacts.xml");
                bool downloaded = cr.Download();
                if (downloaded)
                {
                    getData();
                }
            }
            else
            {
                MessageBox.Show("Not Updating Contacts");
                getData();
            }

        }

我正在寻找一种方法来让数据加载到列表框中,而不告诉用户关闭应用程序并重新加载它。

非常感谢任何帮助。

【问题讨论】:

    标签: xml windows-phone-7 listbox


    【解决方案1】:

    在最后使用 .toList() 将您的 linq 查询转换为结果,就像

      lstContacts.ItemsSource = (from contact in xmlContact.Descendants("contact")
                                                                   select new ContactItem
                                                                   {
                                                                       ImageSource = contact.Element("Image").Value,
                                                                       FName = contact.Element("FName").Value,
                                                                       LName = contact.Element("LName").Value,
                                                                       Extension = contact.Element("Extension").Value,
                                                                       Email = contact.Element("Email").Value,
                                                                       Cell = contact.Element("Cell").Value,
                                                                       Title = contact.Element("TitleName").Value,
                                                                       Dept = contact.Element("deptName").Value,
                                                                       Office = contact.Element("officename").Value,
                                                                       ID = contact.Element("ID").Value
                                                                   }).ToList<ContactItem>();
    

    现在这将执行查询并返回列表,这将能够绑定数据。

    试试这个 (查询 }).ToList();

    【讨论】:

    • 没有? ContactItem 不包含 .toList() 方法,如果我将 itemessource 传递给 IEnumerable.toList(),它没有什么不同。
    • 试试这个(查询 }).ToList();
    • 使用它,它只是通过进入列表项,进入可观察集合,然后进入列表框来添加另一层冗余,而我直接进入列表框。
    • 您将 linq 查询传递给数据源,这是错误的,甚至无法从查询中获取数据。目前它只是一个查询,一旦你强迫他获取数据就会执行。例如,第八个循环或直接调用 tolist 函数。我的事情你需要了解更多关于 linq 查询 linq 查询是如何工作的。希望这会给出答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 2014-04-10
    相关资源
    最近更新 更多