带有 Web 服务的 WindowsPhoneDataBoundApplication
我想合并包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,借助数据绑定和可观察集合是可能的。您可以使用 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.