开发者

WP7 Trouble populating pie chart

I'm having some trouble with populating a pie chart in my WP7 projec开发者_高级运维t. 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;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜