xaml语言建立首个win8 Metro应用,rss阅读器

本实例是来源msdn的Metro开发文档,按着解说一步步来理解的,修改了一点点,拿了博客园本人的博客作为RSS阅读,本实例用来学习还是可以的

参考文档http://msdn.microsoft.com/zh-cn/library/windows/apps/br211380.aspx#Y909 

先看允许结果

                                                    

 

本例子主要有2个类文件和2个xaml文件

第一个类文件FeedData.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



using System.Collections.ObjectModel;

using System.Threading.Tasks;

using Windows.Web.Syndication;



using Windows.Globalization.DateTimeFormatting;







namespace sl

{

// FeedData

// Holds info for a single blog feed, including a list of blog posts (FeedItem)

public class FeedData

{

public string Title { get; set; }

public string Description { get; set; }

public DateTime PubDate { get; set; }



private List<FeedItem> _Items = new List<FeedItem>();

public List<FeedItem> Items

{

get

{

return this._Items;

}

}

}



// FeedItem

// Holds info for a single blog post

public class FeedItem

{

public string Title { get; set; }

public string Author { get; set; }

public string Content { get; set; }

public DateTime PubDate { get; set; }

public Uri Link { get; set; }

}



// FeedDataSource

// Holds a collection of blog feeds (FeedData), and contains methods needed to

// retreive the feeds.

public class FeedDataSource

{

private ObservableCollection<FeedData> _Feeds = new ObservableCollection<FeedData>();

public ObservableCollection<FeedData> Feeds

{

get

{

return this._Feeds;

}

}



public async Task GetFeedsAsync()

{



Task<FeedData> feed1 =

GetFeedAsync(" http://feed.cnblogs.com/blog/u/109818/rss");



this.Feeds.Add(await feed1);

}



private async Task<FeedData> GetFeedAsync(string feedUriString)

{

// using Windows.Web.Syndication;

SyndicationClient client = new SyndicationClient();

Uri feedUri = new Uri(feedUriString);



try

{

SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri);



// This code is executed after RetrieveFeedAsync returns the SyndicationFeed.

// Process it and copy the data we want into our FeedData and FeedItem classes.

FeedData feedData = new FeedData();



feedData.Title = feed.Title.Text;

if (feed.Subtitle.Text != null)

{

feedData.Description = feed.Subtitle.Text;

}

// Use the date of the latest post as the last updated date.

feedData.PubDate = feed.Items[0].PublishedDate.DateTime;



foreach (SyndicationItem item in feed.Items)

{

FeedItem feedItem = new FeedItem();

feedItem.Title = item.Title.Text;

feedItem.PubDate = item.PublishedDate.DateTime;

feedItem.Author = item.Authors[0].Name.ToString();

// Handle the differences between RSS and Atom feeds.

if (feed.SourceFormat == SyndicationFormat.Atom10)

{

feedItem.Content = item.Content.Text;

feedItem.Link = new Uri("http://www.cnblogs.com" + item.Id);

}

else if (feed.SourceFormat == SyndicationFormat.Rss20)

{

feedItem.Content = item.Summary.Text;

feedItem.Link = item.Links[0].Uri;

}

feedData.Items.Add(feedItem);

}

return feedData;

}

catch (Exception)

{

return null;

}

}

}



}

 

第2个类文件DateConverter.cs

主要负责数据的转换

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



using System.Collections.ObjectModel;

using System.Threading.Tasks;

using Windows.Web.Syndication;



using Windows.Globalization.DateTimeFormatting;



namespace sl

{

public class DateConverter : Windows.UI.Xaml.Data.IValueConverter

{

public object Convert(object value, Type targetType, object parameter, string culture)

{

if (value == null)

throw new ArgumentNullException("value", "Value cannot be null.");



if (!typeof(DateTime).Equals(value.GetType()))

throw new ArgumentException("Value must be of type DateTime.", "value");



DateTime dt = (DateTime)value;



if (parameter == null)

{

// Date "7/27/2011 9:30:59 AM" returns "7/27/2011"

return DateTimeFormatter.ShortDate.Format(dt);

}

else if ((string)parameter == "day")

{

// Date "7/27/2011 9:30:59 AM" returns "27"

DateTimeFormatter dateFormatter = new DateTimeFormatter("{day.integer(2)}");

return dateFormatter.Format(dt);

}

else if ((string)parameter == "month")

{

// Date "7/27/2011 9:30:59 AM" returns "JUL"

DateTimeFormatter dateFormatter = new DateTimeFormatter("{month.abbreviated(3)}");

return dateFormatter.Format(dt).ToUpper();

}

else if ((string)parameter == "year")

{

// Date "7/27/2011 9:30:59 AM" returns "2011"

DateTimeFormatter dateFormatter = new DateTimeFormatter("{year.full}");

return dateFormatter.Format(dt);

}

else

{

// Requested format is unknown. Return in the original format.

return dt.ToString();

}

}



public object ConvertBack(object value, Type targetType, object parameter, string culture)

{

string strValue = value as string;

DateTime resultDateTime;

if (DateTime.TryParse(strValue, out resultDateTime))

{

return resultDateTime;

}

return Windows.UI.Xaml.DependencyProperty.UnsetValue;

}

}



}
第一个界面文件BlankPage.xaml文件(包括两段代码)
xaml代码
<Page
x:Class="sl.BlankPage"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local
="using:sl"
xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable
="d">

<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="140" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<!-- Title -->
<TextBlock x:Name="TitleText" Text="{Binding Title}"
VerticalAlignment
="Center" FontSize="48" Margin="56,0,0,0"/>

<!-- Content -->
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" MinWidth="320" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>

<!-- Left column -->
<!-- The default value of Grid.Column is 0, so we do not need to set it
to make the ListView show up in the first column.
-->
<ListView x:Name="ItemListView"
ItemsSource
="{Binding Items}"
Margin
="60,0,0,10"
SelectionChanged
="ItemListView_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Title}"
FontSize
="24" Margin="5,0,0,0" TextWrapping="Wrap" />
<TextBlock Text="{Binding Author}"
FontSize
="16" Margin="15,0,0,0"/>
<TextBlock Text="{Binding Path=PubDate, Converter={StaticResource dateConverter}}"
FontSize
="16" Margin="15,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>


<!-- Right column -->
<!-- We use a Grid here instead of a StackPanel so that the WebView sizes correctly. -->
<Grid DataContext="{Binding ElementName=ItemListView, Path=SelectedItem}"
Grid.Column
="1" Margin="25,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock x:Name="PostTitleText" Text="{Binding Title}" FontSize="24"/>
<WebView x:Name="ContentView" Grid.Row="1" Margin="0,5,20,20"/>
</Grid>
</Grid>
</Grid>

</Page>

C#代码

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;



using System.Collections.ObjectModel;

using System.Threading.Tasks;

using Windows.Web.Syndication;



using Windows.Globalization.DateTimeFormatting;



// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238



namespace sl

{

/// <summary>

/// An empty page that can be used on its own or navigated to within a Frame.

/// </summary>

public sealed partial class BlankPage : Page

{









public BlankPage()

{

this.InitializeComponent();

}



/// <summary>

/// Invoked when this page is about to be displayed in a Frame.

/// </summary>

/// <param name="e">Event data that describes how this page was reached. The Parameter

/// property is typically used to configure the page.</param>

protected override async void OnNavigatedTo(NavigationEventArgs e)

{

FeedDataSource _feedDataSource = App.DataSource;



if (_feedDataSource.Feeds.Count == 0)

{

await _feedDataSource.GetFeedsAsync();

}



this.DataContext = (_feedDataSource.Feeds).First();



}



private void ItemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

FeedItem feedItem = e.AddedItems[0] as FeedItem;

if (feedItem != null)

{

// Navigate the WebView to the blog post content HTML string.

ContentView.NavigateToString(feedItem.Content);

}





}

}

}

 

第4个文件是App.xaml(包括2段代码)

 

xaml代码

<Application

x:Class="sl.App"

xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local
="using:sl">



<Application.Resources>

<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>

<ResourceDictionary>

<local:FeedDataSource x:Key="feedDataSource"/>



<!-- Add the DateConverter here. -->

<local:DateConverter x:Key="dateConverter" />



</ResourceDictionary>

</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

</Application.Resources>



</Application>

 

C#代码

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.ApplicationModel;

using Windows.ApplicationModel.Activation;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;



using System.Collections.ObjectModel;

using System.Threading.Tasks;

using Windows.Web.Syndication;



using Windows.Globalization.DateTimeFormatting;



// The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=234227



namespace sl

{

/// <summary>

/// Provides application-specific behavior to supplement the default Application class.

/// </summary>







sealed partial class App : Application

{

/// <summary>

/// Initializes the singleton application object. This is the first line of authored code

/// executed, and as such is the logical equivalent of main() or WinMain().

/// </summary>

public static FeedDataSource DataSource;



public App()

{

this.InitializeComponent();

DataSource = new FeedDataSource();



}



/// <summary>

/// Invoked when the application is launched normally by the end user. Other entry points

/// will be used when the application is launched to open a specific file, to display

/// search results, and so forth.

/// </summary>

/// <param name="args">Details about the launch request and process.</param>

protected override void OnLaunched(LaunchActivatedEventArgs args)

{

if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)

{

//TODO: Load state from previously suspended application

}



// Create a Frame to act navigation context and navigate to the first page

var rootFrame = new Frame();

rootFrame.Navigate(typeof(BlankPage));



// Place the frame in the current Window and ensure that it is active

Window.Current.Content = rootFrame;

Window.Current.Activate();

}



/// <summary>

/// Invoked when application execution is being suspended. Application state is saved

/// without knowing whether the application will be terminated or resumed with the contents

/// of memory still intact.

/// </summary>

/// <param name="sender">The source of the suspend request.</param>

/// <param name="e">Details about the suspend request.</param>

void OnSuspending(object sender, SuspendingEventArgs e)

{

//TODO: Save application state and stop any background activity



}

}

}


源于参考文档http://msdn.microsoft.com/zh-cn/library/windows/apps/br211380.aspx#Y909

转载于:https://www.cnblogs.com/suguoqiang/archive/2012/03/07/2384394.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/349713.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在扩展Spock时输出给定值

Spock是一个Java测试框架&#xff0c;由GradleWare的软件工程师Peter Niederwieser于2008年创建&#xff0c;它可以促进BDD的发展。 利用这个 例如 &#xff0c;一个故事可以定义为&#xff1a; Story: Returns go to stockAs a store owner In order to keep track of stock…

wsgi

wsgi&#xff0c;通用网关接口。相当于在app与web服务&#xff08;socket服务端&#xff09;之间建立统一连接的规范。 真实开发中的python web程序来说&#xff0c;一般会分为两部分&#xff1a;服务器程序和应用程序。服务器程序负责对socket服务器进行封装&#xff0c;并在请…

Java与Python:哪一种最适合您? [信息图]

通过从应用程序中学习企业APM产品&#xff0c;发现更快&#xff0c;更高效的性能监控。 参加AppDynamics APM导览&#xff01; 在软件开发中&#xff0c;很少有问题比选择编程语言更具分裂性或部落性。 软件开发人员经常以自己选择的工具来强烈地认同自己&#xff0c;将客观事…

零基础学习java------day11------常用API

API概述 API(application Programming Interface, 应用程序编程接口)&#xff0c;是一些预先定义的函数。目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力&#xff0c;而又无需访问源码&#xff0c;或理解内部工作机制的细节 比如我需要在一个程序里面嵌入…

.net 下的集合

集合的操作在编码的时候很常见。但是由于经常使用几种集合。而忽略了一些不常用的集合。在这里我整理下。 首先先了解下接口&#xff1a; 1、IEnumerable&#xff0c;返回一个循环访问集合的枚举器。 2、IEnumerable<T>&#xff0c;返回一个循环访问指定集合T的枚举器。 …

aspects_具有Aspects的Java中的Mixin –用于Scala特性示例

aspectsScala特征允许将新行为混合到一个类中。 考虑两个特征&#xff0c;可以向JPA实体添加审核和与版本相关的字段&#xff1a; package mvcsample.domainimport javax.persistence.Version import scala.reflect.BeanProperty import java.util.Datetrait Versionable {Ver…

TCP服务端实现并发

socket 在 tcp 协议下通信 客户端 import socket ​ # 创建客户端TCP协议通信 c socket.socket() # 与指定服务端握手 c.connect((127.0.0.1, 8080)) ​ # 通信循环 while True:# 向服务端发送信息msg input(>>>)if len(msg) 0:continuec.send(msg.encode(utf-8))#…

问题 1052: [编程入门]链表合并

题目描述已有a、b两个链表&#xff0c;每个链表中的结点包括学号、成绩。要求把两个链表合并&#xff0c;按学号升序排列。 输入第一行&#xff0c;a、b两个链表元素的数量N、M,用空格隔开。 接下来N行是a的数据 然后M行是b的数据 每行数据由学号和成绩两部分组成 输出按照学号…

ssh 看apache_使用Apache KeyedObjectPool的ssh连接池

ssh 看apache我发现org.apache.commons.pool非常有用且健壮&#xff0c;但没有充分记录。 因此&#xff0c;我将在这里帮助您解释如何使用Apache KeyedObjectPool 。 什么是KeyedObjectPool &#xff1f; 它是一个映射&#xff0c;其中包含多种类型的实例池。 可以使用任意键访…

问题 1066: 2004年秋浙江省计算机等级考试二级C 编程题(2)

题目描述输入一个正数x和一个正整数n&#xff0c;求下列算式的值。要求定义两个调用函数&#xff1a;fact(n)计算n的阶乘&#xff1b;mypow(x,n)计算x的n次幂&#xff08;即xn&#xff09;&#xff0c;两个函数的返回值类型是double。 x - x2/2! x3/3! ... (-1)n-1xn/n! 输出…

Ubuntu16.04下安装多版本cuda和cudnn

Ubuntu16.04下安装多版本cuda和cudnn 原文 https://blog.csdn.net/tunhuzhuang1836/article/details/79545625 前言因为之前针对Pytorch&#xff0c;caffe&#xff0c;torch等&#xff0c;装了cuda8.0和对应cudnn5.1&#xff0c;但是最近在装MxNet的时候&#xff0c;发现官网上…

什么是javax.ws.rs.core.context? [ 第1部分 ]

如何使用Context批注 JAX-RS API提供了一种非常方便的机制&#xff0c;可以将各种有用的资源&#xff08;例如HTTP标头&#xff09;注入到端点中。 Context注释是一个通用注释&#xff0c;它注入以下对象的实例&#xff1a; HttpHeaders- > HTTP标头参数和值 UriInfo- >…

spring的事件

理论 异步的实现方式可以使用事件&#xff0c;或者异步执行&#xff1b; spring中自带了事件的支持&#xff0c;核心是ApplicationEventPublisher; 事件包括三个要点&#xff1a; 事件的定义&#xff1b;事件监听的定义&#xff1b;发布事件&#xff1b;实战 代码路径&#xff…

[多项式算法]多项式求逆 学习笔记

多项式求逆 和整数的乘法逆元定义类似&#xff0c;对于多项式\(A(x)B(x)1\)&#xff0c;则称\(A(x),B(x)\)互为乘法逆元。 \(A(x)\)存在乘法逆元的充要条件是\([x^0]A(x)\)存在乘法逆元。 现在思考如何用\(O(n\log n)\)的时间计算\(A(x)\)的乘法逆元&#xff1a; 考虑倍增&…

java jax-rs_在Java EE 6中将Bean验证与JAX-RS集成

java jax-rsJavaBeans验证&#xff08;Bean验证&#xff09;是Java EE 6平台的一部分提供的新验证模型。 约束通过以JavaBeans组件&#xff08;例如托管Bean&#xff09;的字段&#xff0c;方法或类上的注释形式的约束来支持Bean验证模型。 javax.validation.constraints包中提…

ubuntu16.04+cuda9.0_cudnn7.5+tensorflow-gpu==1.12.0

1、查找可用的tensorflow源&#xff0c;该命令运行后终端会输出所有可用的源 anaconda search -t conda tensorflow2、这里name是上一步中输出源的tensorflow name栏的名称&#xff0c;show命令会在终端输出该源具体的信息和下载需要的指令,执行后如图&#xff1a; anaconda sh…

介绍OpenHub框架

本文介绍了OpenHub框架 -基于Apache Camel的新的开源集成解决方案。 本文回答了一些问题&#xff0c;为什么您应该关心另一个集成框架&#xff0c;强弱属性以及如何使用OpenHub启动新项目。 OpenHub框架是Apache Camel&#xff0c;但经过改进…… 当然&#xff0c;您只能使用A…

问题 1072: 汽水瓶

题目描述有这样一道智力题&#xff1a;“某商店规定&#xff1a;三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶&#xff0c;她最多可以换多少瓶汽水喝&#xff1f;”答案是5瓶&#xff0c;方法如下&#xff1a;先用9个空瓶子换3瓶汽水&#xff0c;喝掉3瓶满的&#xff0…

呼叫我或异步REST

本文是使用Spring Boot Java 8创建的工作正常的异步REST应用程序的非常简单的示例。SpringBoot使得开发Web应用程序几乎非常容易&#xff0c;但是为了简化任务&#xff0c;我从Spring存储库中举了一个例子&#xff0c;称为rest- service &#xff0c;将其分叉到我自己的存储库…

NOIP模拟测试22「位运算」

范围n-----$100000$ m $30$ 输出方案 这是一个很好的$dp$题 首先我们应该看出来一条性质只要你最后有方案达到$n$个$1$&#xff0c;那么你可以达到任何一种$n$个$1$的情况 例如 你最后可以达到$3$个$1$ 那么你可以达到$11100 $ $ 01110$ $01011$ $01101$等方案 证明&a…