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 >9736994956</ id >
    < text >【12星座一定要娶回家的理由】白羊座:可以陪你看球;金牛座:细心体贴;双子座:胸襟宽大;巨蟹座:纯真;狮子座:善解人意;处女座:安定;天秤座:真诚;天蝎座:浪漫;射手座:理解万岁;魔羯座:安全感;水瓶座:坚实的后盾;双鱼座:温柔。(你想娶哪个星座MM呢?)</ 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 >★ 好评率100%★ 一���有态度的热门资讯平台——分享全球最新鲜、时尚、有趣的话题。关注@全球热门排行榜 让我们像家人一样度过精彩的每一天!</ 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 >最火的丝袜,万人推荐,穿上立刻显瘦,还有燃脂功能!众多明星在使用,大家来看看http://t.cn/htlFEw</ 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
        };
?
如果xml中指定了xmlns,则
?
foreach (XElement feedPost in xdoc.Elements(XName.Get( "status" , "http://api.renren.com/1.0/" ))) 即可<BR>

2.数据绑定

首先在界面上做绑定

?
< 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();
});

关于INotifyPropertyChanged接口,虽然看Jake Lin的视频上说数据源提供类在oneway,twoway一定要实现该接口,但是个人感觉这只是一个发生改变时的通知,如果绑定数据只显示而很少或基本不更改,那么没有必要实现。--(这是我以前的想法,结果后来遇到数据第一次不能刷新的情况后来才发现是没有实现该接口的问题,罪过啊)

3. 关于WP7的数据绑定。

?
1. <TextBlock .. Text= "{Binding ElementName=slider, Path=Value}" />
此处绑定的是Name为slider的Slider控件, Path 为属性,不用Property而用Path的原因是path可以指定一个序列 。 如 Children[0].Value
 
2. 用代码表示
Binding binding = new Binding();
binding.ElementName = "slider" ;
binding.Path = new PropertyPath( "value" );
 
txtblk.SetBinding(TextBlock.TextProperty, binding);
绑定要求是 txtblk 是FrameworkElement, 而 TextBlock.TextProperty为dependency property.
 
3.Binding convertors
Binding的Converter属性是IValueConverter类型的,需要实现Convert 和 ConvertBack 两个方法。 
另外Binding类还定义了ConverterParameter属性,用于格式化,支持标准的String.Format方法。
 
text= "{Binding ElementName=slider, Path=value, Converter={StaticResource stringFormat}, ConverterParameter='{0:F2}'}"
 
4.Relative Source
用于绑定同一个元素的不同属性
{Binding RelativeSouce={RelativeSouce self}, Path=FontFamily}

转载于:https://www.cnblogs.com/quan448600140/archive/2012/06/26/2563088.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值