设为首页 收藏本站
查看: 1356|回复: 0

一个简易的 Windows 8 RSS 阅读器

[复制链接]

尚未签到

发表于 2015-5-22 07:23:11 | 显示全部楼层 |阅读模式
  先上运行截图:
DSC0000.png
  简单说明:右侧主要内容的显示使用了浏览器控件WebView,另外,一些说明放在了代码注释中。
  本应用只有一张页面MainPage
  前台代码如下:


DSC0001.gif DSC0002.gif XAML


1
9     
10         
11            
12            
13            
14         
15         30
16     
17
18     
19         
20            
21            
22         
23         
24            
25                 
26                 
27                 
28                 
29                 
30                 
31            
32            
33            
34            
35                 
36                 
37            
38            
39                 
40                 
41            
42            
43                 
44                 
45            
46            
47                 
48                     
49                        
50                     
51                 
52            
53         
54         
55            
56                 
57                 
58            
59            
60            
61         
62     
63
  后台代码如下:


C#


  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Threading.Tasks;
  6 using Windows.Foundation;
  7 using Windows.Foundation.Collections;
  8 using Windows.Storage;
  9 using Windows.UI.Xaml;
10 using Windows.UI.Xaml.Controls;
11 using Windows.UI.Xaml.Controls.Primitives;
12 using Windows.UI.Xaml.Data;
13 using Windows.UI.Xaml.Input;
14 using Windows.UI.Xaml.Media;
15 using Windows.UI.Xaml.Navigation;
16 using Windows.Web.Syndication;
17
18 namespace Win8RssReader
19 {
20     public sealed partial class MainPage : Page
21     {
22         SyndicationClient syndicationClient;
23         SyndicationFeed currentFeed;
24
25         ///
26         /// WebView在打开target="_blank"的超链接时会打开电脑上的浏览器,为了避免这种情况,这段js可通过更改DOM,使所有超链接的target="_self"。
27         ///
28         string MyJs { get; set; }
29
30         public MainPage()
31         {
32             this.InitializeComponent();
33         }
34
35         async protected override void OnNavigatedTo(NavigationEventArgs e)
36         {
37             MyJs = await GetMyJs();
38         }
39
40         private async Task GetMyJs()
41         {
42             StorageFile jsFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///js.txt"));
43             string jsText = await FileIO.ReadTextAsync(jsFile);
44             return jsText;
45         }
46
47         private async void btnGetFeed_Click(object sender, RoutedEventArgs e)
48         {
49             string feedUriString = txtFeedUri.Text;
50             await GetFeedAsync(feedUriString);
51             DisplayFeed();
52         }
53
54         private async Task GetFeedAsync(string feedUriString)
55         {
56             Uri uri;
57             if (!Uri.TryCreate(feedUriString.Trim(), UriKind.Absolute, out uri))
58             {
59                 txtMsg.Text = "url错误";
60                 return;
61             }
62             if (syndicationClient == null)
63             {
64                 syndicationClient = new SyndicationClient();
65             }
66             syndicationClient.BypassCacheOnRetrieve = true;
67             syndicationClient.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
68             txtMsg.Text = "开始下载...";
69             try
70             {
71                 currentFeed = await syndicationClient.RetrieveFeedAsync(uri);
72                 txtMsg.Text = "下载完成";
73             }
74             catch (Exception ex)
75             {
76                 txtMsg.Text = ex.Message;
77             }
78         }
79
80         private void DisplayFeed()
81         {
82             if (currentFeed != null)
83             {
84                 ISyndicationText title = currentFeed.Title;
85                 txtFeedTitle.Text = title != null ? title.Text : "(没有标题)";
86                 txtCount.Text = currentFeed.Items.Count.ToString();
87                 listTitle.ItemsSource = currentFeed.Items;
88                 listTitle.SelectedIndex = 0;//选中第0条,触发SelectionChanged事件。
89             }
90         }
91
92         private void listTitle_SelectionChanged(object sender, SelectionChangedEventArgs e)
93         {
94             var item = (SyndicationItem)listTitle.SelectedValue;
95             DisplayCurrentItem(item);
96         }
97
98         private void DisplayCurrentItem(SyndicationItem item)
99         {
100             if (item != null)
101             {
102                 txtItemTitle.Text = item.Title != null ? item.Title.Text : "(没有标题)";
103                 string content = "(没有内容)";
104                 if (item.Content != null)
105                 {
106                     content = item.Content.Text;
107                 }
108                 else if (item.Summary != null)
109                 {
110                     content = item.Summary.Text;
111                 }
112                 content += MyJs;
113                 webViewContent.NavigateToString(content);
114             }
115         }
116     }
117 }
  引用文件js.txt中的内容如下:




//WebView在打开target="_blank"的超链接时会打开电脑上的浏览器,为了避免这种情况,这段js可通过更改DOM,使所有超链接的target="_self"。
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
links.target = "_self";
}

  
  解决方案打包下载 Win8RssReader.rar
  
  题外话:
  感觉现在做Windows Store App开发的很少哦,真心希望能和感兴趣的朋友们交流交流。
  欢迎留言 ^_^

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69319-1-1.html 上篇帖子: 菜鸟学Windows Phone 8开发(1)——创建第一个应用程序 下篇帖子: 重新想象 Windows 8 Store Apps (68)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表