WP7 腾讯/新浪微博 设计 -- 解析XML数据并绑定
1. Silverlight中解析XML数据
?
在SL中使用LINQ TO XML解析XML数据是个不错的方案。
命名空间:using system.xml.linq;
首先,微博API返回数据可以有2种,json或者xml. 本例中采取xml来解析。
范例XML如下(新浪微博)。
?
<statuses> <status> <created_at>Wed Apr 27 20:12:46 +0800 2011</created_at> <id>marcoweaver</id> <text>做html,css培训,过了一把老师瘾。</text> <source> <a href="http://weibo.com">新浪微博</a> </source> <favorited>false</favorited> <truncated>false</truncated> <geo /> <in_reply_to_status_id /> <in_reply_to_user_id /> <in_reply_to_screen_name /> <mid>5600235199709807760</mid> <user> <id>1252373132</id> <screen_name>全球热门排行榜</screen_name> <name>全球热门排行榜</name> <province>44</province> <city>1</city> <location>北京</location> <description></description> <url>http://1</url> <profile_image_url>http://tp1.sinaimg.cn/1252373132/50/1290081552/1</profile_image_url> <domain>saviourlove</domain> <gender>m</gender> <followers_count>1292095</followers_count> <friends_count>1240</friends_count> <statuses_count>7134</statuses_count> <favourites_count>4338</favourites_count> <created_at>Tue Sep 08 00:00:00 +0800 2009</created_at> <following>false</following> <verified>false</verified> <allow_all_act_msg>true</allow_all_act_msg> <geo_enabled>true</geo_enabled> </user> <retweeted_status> <created_at>Wed Apr 27 14:11:31 +0800 2011</created_at> <id>9720751183</id> <text>智城</text> <source> <a href="http://weibo.com">新浪微博</a> </source> <favorited>false</favorited> <truncated>false</truncated> <geo /> <in_reply_to_status_id /> <in_reply_to_user_id /> <in_reply_to_screen_name /> <thumbnail_pic>http://ww4.sinaimg.cn/thumbnail/78c29ec1jw1dgn178wnfcj.jpg</thumbnail_pic> <bmiddle_pic>http://ww4.sinaimg.cn/bmiddle/78c29ec1jw1dgn178wnfcj.jpg</bmiddle_pic> <original_pic>http://ww4.sinaimg.cn/large/78c29ec1jw1dgn178wnfcj.jpg</original_pic> <mid>5600142106459294297</mid> <user> <id>2026020545</id> <screen_name>只分享快乐</screen_name> <name>只分享快乐</name> <province>32</province> <city>1</city> <location>江苏 南京</location> <description /> <url /> <profile_image_url>http://tp2.sinaimg.cn/2026020545/50/1300101443/0</profile_image_url> <domain /> <gender>f</gender> <followers_count>30</followers_count> <friends_count>45</friends_count> <statuses_count>13</statuses_count> <favourites_count>0</favourites_count> <created_at>Mon Mar 14 00:00:00 +0800 2011</created_at> <following>false</following> <verified>false</verified> <allow_all_act_msg>false</allow_all_act_msg> <geo_enabled>true</geo_enabled> </user> </retweeted_status> </status> <!--若干个status--> </status>?
根据XML格式设计对应的实体类,分别为Status 和 User,属性对应节点,很简单,这里不赘述。
linq解析格式如下;其中content为服务器返回的字符串。
?
XElement doc = XElement.Parse(content); var friendTimeLine = from p in doc.Descendants("status") select new Status { Created_at = p.Element("created_at").Value, StatusId = p.Element("id").Value };
?
foreach (XElement feedPost in xdoc.Elements(XName.Get("status", "http://api.renren.com/1.0/"))) ?<controls:PivotItem Header="我的主页"><!--Triple line list no text wrapping--> <ListBox x:Name="SecondListBox" ItemsSource="{Binding Mode=OneWay}" Margin="0,0,-12,0"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Margin="0,0,0,17"> <TextBlock Text="{Binding Created_at}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/> <TextBlock Text="{Binding StatusId}" TextWrapping="NoWrap" Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox></controls:PivotItem>?XElement doc = XElement.Parse(content); var friendTimeLine = from p in doc.Descendants("status") select new Status { Created_at = p.Element("created_at").Value, StatusId = p.Element("id").Value }; Dispatcher.BeginInvoke(() =>{ SecondListBox.DataContext = friendTimeLine.ToList();});<TextBlock .. Text="{Binding ElementName=slider, Path=Value}"/>?Binding binding = new Binding();binding.ElementName = "slider";binding.Path = new PropertyPath("value");
text="{Binding ElementName=slider, Path=value, Converter={StaticResource stringFormat}, ConverterParameter='{0:F2}'}"?
4.Relative Source
用于绑定同一个元素的不同属性
5.DataContext
可以省略一些重复的代码,比如 StackPanel下2个TextBlock text=
"{Binding Source={StaticResource clock}, Path=Hour}"
另外一个也需要重复写 Source.
但是如果给StackPanel指定DataContext之后,这两个TextBlock就可以只指定Text=
"{Binding Hour}"
就OK。