WP7 填充饼图时遇到问题

发布于 2024-10-09 21:45:44 字数 750 浏览 5 评论 0原文

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

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

发布评论

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

评论(1

月光色 2024-10-16 21:45:44

我的猜测是这一行存在编译错误:

XElement Quota = XDocument.Descendants("traffic").Attributes("quota");

Descendants("traffic") 的结果是 IEnumerable,而不是 XElement。在上面的行中,您已经获得了该可枚举的 First,这就是您想要的项目,不是吗?

配额行应该是:

XElement Quota = Traffic.Attributes("quota");

风格方面,大多数人将局部变量设置为小写,例如 trafficquotadata 以将它们与类级别区分开来属性和成员。

更新:看起来Attributes("quota")返回IEnumerable,因此配额行应该是:

XAttribute Quota = Traffic.Attributes("quota").FirstOrDefault();

或者简化为:

var traffic = XDocument.Descendants("traffic").First();   
var quota = traffic.Attributes("quota").FirstOrDefault();

I不想变得刻薄,但是修复这样的编译器错误应该是您不必来到 stackoverflow 的事情。编译器错误本身告诉您问题是什么:该方法返回的类型与您所说的不同。使用 var 可以简化其中的一些工作。

my guess is this line has the compile error:

XElement Quota = XDocument.Descendants("traffic").Attributes("quota");

the result of Descendants("traffic") is an IEnumerable, not an XElement. in the line above that you're already getting First of that enumerable, which is the item you want, isn't it?

the quota line should be:

XElement Quota = Traffic.Attributes("quota");

Style wise, most people make local variables lower cased, like traffic and quota and data to distinguish them from class level properties and members.

Update: it looks like Attributes("quota") returns IEnumerable<XAttribute>, so that quota line should be:

XAttribute Quota = Traffic.Attributes("quota").FirstOrDefault();

or to simplify:

var traffic = XDocument.Descendants("traffic").First();   
var quota = traffic.Attributes("quota").FirstOrDefault();

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.

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