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
用于绑定同一个元素的不同属性
可以省略一些重复的代码,比如 StackPanel下2个TextBlock text=
"{Binding Source={StaticResource clock}, Path=Hour}"
另外一个也需要重复写 Source.
|
但是如果给StackPanel指定DataContext之后,这两个TextBlock就可以只指定Text=
"{Binding Hour}"
就OK。
分享到:
相关推荐
该项目已被https://github.com/Automattic/wp-api-console取代,该项目现在通过developer.wordpress.com为API控制台提供动力。 开发黑客需要node.js,为您的系统安装node.js。 (例如brew install节点)。 要启动并...
$ wget https://raw.githubusercontent.com/varunsridharan/wp-ajaxer/master/class-ajaxer.php 样品用量 class BoilerPlate_Ajax extends Varunsridharan \ WordPress \ Ajaxer { /** * Ajax Action Prefix *
使用WordPress撰写中文文章的时候,如果说在固定链接中选择加入一个%postname%项,生成的url就会因为含有中文而无法被支持,网站显示404。这是需要进行更改后的文件,可以解决问题。
3.将wp-content/plugins/wp-dbmanager目录中的 htaccess.txt修改为:.htaccess ,并把.htaccess 文件移动到:wp-content/backup-db/目录下 (你也可以下载我们为你修改好的:.htaccess文件压缩包,解压后上传到wp-...
来源:,涉及Cookie的使用方法操作方
S-TUNE_Ver-2_1_11_0--simplified_Chinesed SV-X5系列伺服电机软件
WP7新浪微博SDK源码 源码介绍: 支持 Oauth 1.0 和 xAuth 两种授权模式。 下载包中包含源代码,示例代码。 新浪微博是一个由新浪网推出,提供微型博客服务的类Twitter网站。 用户可以通过网页、WAP页面、手机短信、...
新浪微博的WP7 SDK。帮助你快速链接新浪微博API
解压ZIP文档,并上传至/wp-content/plugins/目录下激活即可。 注:如过您的Wordpress并非安装在网站根目录下,比如是在根目录下的blog目录中,请编辑nicEdit.js文件的第30行为: iconsPath : ‘/blog/wp-content/...
/bin/cp /home/chenlong/blog/wp-includes/js/common.js /home/chenlong/Publish/blog/2010-05-21/patch/home/chenlong/blog/wp-includes/js/common.js === cmd === :/home/chenlong/patch_sh/apply_cp.sh /home/...
WP29的全称为联合国世界车辆法规协调论坛(简称为UN/WP29),WP29的工作是我国汽车行业参加的主要国际汽车技术法规工作,对我国汽车产业和国际贸易的发展有着至关重要的作用。
使用端点:“ / wp-json / wp / v2 / multiple-post-type” 清单: GET请求到/wp-json/wp/v2/multiple-post-type?&type[]=post&type[]=page 用“约我”来获取页面: GET /wp-json/wp/v2/multiple-post-type?...
PHP实例开发源码—worldpress 新浪微博同步插件 WP-Connect For WordPress.zip PHP实例开发源码—worldpress 新浪微博同步插件 WP-Connect For WordPress.zip PHP实例开发源码—worldpress 新浪微博同步插件 WP-...
UNIX Programming FAQ 中文版 常见问题的解析
Foxlogin是一款快速让你的WordPress网站接入微博和QQ快捷登陆的插件,让你的站点用户绑定腾讯QQ或新浪微博来进行登录的wordpress中文插件。 【开启QQ快速登录】:开启QQ快速登录开关设置 【QQ账户APP ID】:填写...
1.下载Automatic SEO Links插件,将压缩包解压后,把文件夹上传到wp-content/plugins/目录下,登录WordPress管理后台,点击“Plugins”找到上传的插件,激活该插件。 2.插件激活后,在后台的左侧栏中“setting”--->...
Altman第三方腾讯微博WP7芒果版 v2.1源码 程序描述: altman是基于开发接口的第三方腾讯微博客户端,可以运行在windows phone 7 mango作业系统的智能手机上,全新的版本有简洁清新的界面,手势操作和它所采用的...
基于ThinkPHP开发的开发的一款医疗门诊系统源码,是一款专业用于医疗进销存数据统计的管理系统, 功能齐全,使用方便。...https://czqixidi.com/wp-content/uploads/2023/08/image-120-442x1024.png
Avada-zh_CN.mo-这是主题语言包,上传到:wp-content/languages/themes/ fusion-builder-zh_CN.mo-这是插件语言包,上传到:wp-content/languages/plugins/ fusion-core- zh_CN.mo-这是插件语言包,上传到:wp-...
这个是一元云购安装的视频教程,从域名解析绑定开始,到网站安装完成整个过程!<!-- wp:paragraph --> 之前给大家录制了一个免费版云购系统的教程,那个是用服务器宝塔环境安装的,但是很多演示源码的用户,舍不得...