`
thierry.xing
  • 浏览: 654679 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
580fa9c1-4a0c-3f40-a55a-c9256ce73302
Sencha Touch中...
浏览量:0
社区版块
存档分类
最新评论

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
        };

 

如果xml中指定了xmlns,则

 

foreach (XElement feedPost in xdoc.Elements(XName.Get("status", "http://api.renren.com/1.0/"))) 
 
即可

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
用于绑定同一个元素的不同属性

5.DataContext
可以省略一些重复的代码,比如 StackPanel下2个TextBlock text= "{Binding Source={StaticResource clock}, Path=Hour}" 另外一个也需要重复写 Source.
但是如果给StackPanel指定DataContext之后,这两个TextBlock就可以只指定Text= "{Binding Hour}" 就OK。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics