带有 Web 服务的 WindowsPhoneDataBoundApplication

发布于 2024-11-25 18:39:59 字数 2487 浏览 1 评论 0原文

我想合并包含 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();
        }
    }

I want to merge between my code that contains the Web service and the code of WindowsPhoneDataBound Application.Is it possible? and how?
My Code:

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2024-12-02 18:39:59

当然,借助数据绑定和可观察集合是可能的。您可以使用 ObservableCollection,而不是每次获取新元素时都添加新的列表框项目。将列表框的数据源设置为可观察集合。

使用ObservableCollection的好处是,每当您从集合中添加或删除元素时,列表框都会自动反映该情况。因此无需手动添加或删除listboxItems。如果您的 listboxItem 有自定义布局,那么您可以创建一个 dataTemplate 并将数据绑定到需要在 listboxItem 中显示的元素。

Of course it is possible with the help of data binding and observable collections. Instead of adding new list box items every time you get a new element, you can use an ObservableCollection. Set the data source of the listbox to the observable collection.

The benefit of using ObservableCollection is that whenever you add or remove an element from your collection, your listbox will automatically reflect that. So no need to manually add or remove listboxItems. If your listboxItem has custom layout, then you can create a dataTemplate and bind data to the elements which needs to be displayed in the listboxItem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文