WP7 填充饼图时遇到问题
我在 WP7 项目中填充饼图时遇到一些问题。目前,我的代码如下。我尝试了几种不同的方法来从 xml Web 服务取回数据,但没有成功。谁能看到我做错了什么吗?
我现在遇到的错误是“无法将类型‘System.Collections.Generic.IEnumerable’隐式转换为‘System.Xml.Linq.XElement’。存在显式转换(是否缺少强制转换?)”
XDocument XDocument = XDocument.Load(new StringReader(e.Result));
XElement Traffic = XDocument.Descendants("traffic").First();
XElement Quota = XDocument.Descendants("traffic").Attributes("quota");
ObservableCollection<PieChartItem> Data = new ObservableCollection<PieChartItem>()
{
new PieChartItem {Title = "Traffic", Value = (double)Traffic},
new PieChartItem {Title = "Quota", Value = (double)Quota},
};
pieChart1.DataSource = Data;
I'm having some trouble with populating a pie chart in my WP7 project. At the moment, my code is as follows below. I've tried a few different ways to bring the data back from an xml web service but no luck. Can anyone see what I have done wrong?
The error I'm getting right now is, "Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Xml.Linq.XElement'. An explicit conversion exists (are you missing a cast?)"
XDocument XDocument = XDocument.Load(new StringReader(e.Result));
XElement Traffic = XDocument.Descendants("traffic").First();
XElement Quota = XDocument.Descendants("traffic").Attributes("quota");
ObservableCollection<PieChartItem> Data = new ObservableCollection<PieChartItem>()
{
new PieChartItem {Title = "Traffic", Value = (double)Traffic},
new PieChartItem {Title = "Quota", Value = (double)Quota},
};
pieChart1.DataSource = Data;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是这一行存在编译错误:
Descendants("traffic")
的结果是IEnumerable
,而不是XElement
。在上面的行中,您已经获得了该可枚举的First
,这就是您想要的项目,不是吗?配额行应该是:
风格方面,大多数人将局部变量设置为小写,例如
traffic
和quota
和data
以将它们与类级别区分开来属性和成员。更新:看起来
Attributes("quota")
返回IEnumerable
,因此配额行应该是:或者简化为:
I不想变得刻薄,但是修复这样的编译器错误应该是您不必来到 stackoverflow 的事情。编译器错误本身告诉您问题是什么:该方法返回的类型与您所说的不同。使用
var
可以简化其中的一些工作。my guess is this line has the compile error:
the result of
Descendants("traffic")
is anIEnumerable
, not anXElement
. in the line above that you're already gettingFirst
of that enumerable, which is the item you want, isn't it?the quota line should be:
Style wise, most people make local variables lower cased, like
traffic
andquota
anddata
to distinguish them from class level properties and members.Update: it looks like
Attributes("quota")
returnsIEnumerable<XAttribute>
, so that quota line should be:or to simplify:
I don't want to be mean, but fixing compiler errors like this should be something you shouldn't have to come to stackoverflow for. The compiler error itself is telling you what the problem is: the method returns a type other than what you said it does. Using
var
can simplify some of that.