【问题标题】:WindowsPhoneDataBoundApplication with web service带有 Web 服务的 WindowsPhoneDataBoundApplication
【发布时间】:2011-07-22 10:25:47
【问题描述】:

我想在包含 Web 服务的代码和 WindowsPhoneDataBound 应用程序的代码之间合并。可以吗?如何? 我的代码:

   private void button1_Click(object sender, RoutedEventArgs e)
    {
        Uri url = new Uri("http://www.google.com/ig/api?weather=" + textBoxVille.Text, UriKind.Absolute);
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        client.DownloadStringAsync(url);
    }
    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            StringReader stream = new StringReader(e.Result);
            XmlReader reader = XmlReader.Create(stream);

            reader.MoveToContent();

            while (reader.Read())
            {
                switch (reader.Name)
                {
                    case ("day_of_week"):
                        {
                            listBox1.Items.Add(new ListBoxItem()
                            {
                                Content = reader.GetAttribute("data")
                            });
                        } break;
                    case ("low"):
                        {
                            listBox1.Items.Add(new ListBoxItem()
                            {
                                Content = reader.GetAttribute("data")
                            });
                        } break;
                    case ("high"):
                        {
                            listBox1.Items.Add(new ListBoxItem()
                            {
                                Content = reader.GetAttribute("data")
                            });
                        } break;
                    case ("icon"):
                        {
                            listBox1.Items.Add(new ListBoxItem()
                            {
                                Content = reader.GetAttribute("data")
                            });
                        } break;

                    case ("condition"):
                        {
                            listBox1.Items.Add(new ListBoxItem()
                            {
                                Content = reader.GetAttribute("data")
                            });
                        } break;
                    case ("weather"):
                        break;
                }
            }
            reader.Close();
        }
    }

【问题讨论】:

    标签: web-services windows-phone-7 listboxitem databound


    【解决方案1】:

    当然,借助数据绑定和可观察集合是可能的。您可以使用 ObservableCollection,而不是每次获得新元素时都添加新的列表框项。将列表框的数据源设置为可观察集合。
    使用 ObservableCollection 的好处是,每当您从集合中添加或删除元素时,您的列表框都会自动反映这一点。因此无需手动添加或删除 listboxItems。如果您的 listboxItem 具有自定义布局,那么您可以创建一个 dataTemplate 并将数据绑定到需要在 listboxItem 中显示的元素。

    【讨论】:

      猜你喜欢
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多