一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

一步一步學(xué)Silverlight :數(shù)據(jù)與通信之ADO.NET Data Services

概述

Silverlight 2 Beta 1版本發(fā)布了,無論從Runtime還是Tools都給我們帶來了很多的驚喜,如支持框架語言Visual Basic, Visual C#, IronRuby, IronPython,對JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步學(xué)Silverlight 2系列》文章將從Silverlight 2基礎(chǔ)知識、數(shù)據(jù)與通信、自定義控件、動畫、圖形圖像等幾個方面帶您快速進(jìn)入Silverlight 2開發(fā)。

本文將簡單介紹在Silverlight 2中如何調(diào)用ADO.NET Data Services。

準(zhǔn)備知識

由于ADO.NET Data Services是在ASP.NET 3.5 Extensions中,所以在開始本文示例之前,首先要安裝一下ASP.NET 3.5 Extensions最新版本,你可以從這里下載。安裝完成后,在添加新項(xiàng)對話框中應(yīng)該能夠看到ADO.NET Data Service項(xiàng):

TerryLee_Silverlight2_0077

ADO.NET Data Service允許應(yīng)用程序把數(shù)據(jù)以服務(wù)的形式公開,這樣我們就可以通過瀏覽器來直接訪問數(shù)據(jù),它支持開放的業(yè)界標(biāo)準(zhǔn),如AtomPub和JSON。它支持標(biāo)準(zhǔn)的HTTP動作如POST、GET、PUT、DELETE,用來完成數(shù)據(jù)的創(chuàng)建、更新、刪除和讀取。ADO.NET Data Service的知識這里不再多說,大家可以去查看相關(guān)的資料。

簡單示例

如果大家看了前面三篇文章的話,可能對于下面的這個界面已經(jīng)很煩了,不過在本文我會仍然采用這個示例進(jìn)行演示:)

TerryLee_Silverlight2_0065

建立完Silverlight 2項(xiàng)目之后,我們在Web項(xiàng)目中添加一個Post類:

public class Post{    public int Id { get; set; }    public string Title { get; set; }    public string Author { get; set; }}

我們用Id作為Post的主鍵,這里需要添加對于Microsoft.Data.Web.dll程序集的引用,位于<盤符>/Program Files/Reference Assemblies/Microsoft/Framework/ASP.NET 3.5 Extensions下面,引入命名空間using Microsoft.Data.Web,并且為Id加上[DataWebKey]特性,最終完成后代碼應(yīng)該如下:

public class Post{    [DataWebKey]    public int Id { get; set; }    public string Title { get; set; }    public string Author { get; set; }}

再添加一個Blog類,它有一個返回類型為IQueryable<Post>的屬性Posts:

public class Blog{    public Blog()    {        _post.Add(new Post { Id = 1, Title = "一步一步學(xué)Silverlight 2系列(13):數(shù)據(jù)與通信之WebRequest", 
Author = "TerryLee" }); _post.Add(new Post { Id = 2, Title = "一步一步學(xué)Silverlight 2系列(12):數(shù)據(jù)與通信之WebClient",
Author = "TerryLee" }); _post.Add(new Post { Id = 3, Title = "一步一步學(xué)Silverlight 2系列(11):數(shù)據(jù)綁定", Author = "TerryLee" }); _post.Add(new Post { Id = 4, Title = "一步一步學(xué)Silverlight 2系列(10):使用用戶控件",
Author = "TerryLee" }); _post.Add(new Post { Id = 5, Title = "一步一步學(xué)Silverlight 2系列(9):使用控件模板", Author = "TerryLee" }); _post.Add(new Post { Id = 6, Title = "一步一步學(xué)Silverlight 2系列(8):使用樣式封裝控件觀感",
 Author = "TerryLee" }); } List<Post> _post = new List<Post>(); public IQueryable<Post> Posts { get { return _post.AsQueryable<Post>(); } }}

 

 

添加一個ADO.NET Data Service,取名BlogDataService.svc:

TerryLee_Silverlight2_0077

實(shí)現(xiàn)服務(wù),讓它繼承于泛型的WebDataService,并且設(shè)置訪問權(quán)限。

public class BlogDataService : WebDataService<Blog>{    public static void InitializeService(IWebDataServiceConfiguration config)    {        config.SetResourceContainerAccessRule("*", ResourceContainerRights.AllRead);    }}

現(xiàn)在我們的服務(wù)端就完成了,現(xiàn)在我們可以在瀏覽器中訪問BlogDataService.svc,應(yīng)該可以看到如下界面:

TerryLee_Silverlight2_0078

 

 

現(xiàn)在還看不到所有的Posts,我們可以在地址欄中輸入http://localhost:8081/BlogDataService.svc/Posts,瀏覽器會默認(rèn)為Feed打開,可以查看源代碼,將會看到所有內(nèi)容,XML內(nèi)容如下(只列出片段):

<?xml version="1.0" encoding="utf-8" standalone="yes"?><feed xml:base="http://localhost:8081/BlogDataService.svc/" ......>  <id>http://localhost:8081/BlogDataService.svc/Posts</id>  <updated />  <title>Posts</title>  <link rel="self" href="Posts" title="Posts" />  <entry adsm:type="TerryLee.SilverlightWithDataServiceDemoWeb.Post">    <id>http://localhost:8081/BlogDataService.svc/Posts(1)</id>    <updated />    <title />    <author>      <name />    </author>    <link rel="edit" href="Posts(1)" title="Post" />    <content type="application/xml">      <ads:Id adsm:type="Int32">1</ads:Id>      <ads:Title>一步一步學(xué)Silverlight 2系列(13):數(shù)據(jù)與通信之WebRequest</ads:Title>      <ads:Author>TerryLee</ads:Author>    </content>  </entry>

如果要查看某一條文章的內(nèi)容,可以輸入http://localhost:8081/BlogDataService.svc/Posts(2)進(jìn)行查看,如下圖所示。

TerryLee_Silverlight2_0079

 

 

當(dāng)然還可以進(jìn)行其他的查詢,使用filter和orderby等,如http://localhost:8081/BlogDataService.svc/Posts?$filter=Id eq 1&$orderby=Id,這里不在介紹。至此我們的數(shù)據(jù)服務(wù)端就算完成了。下面再實(shí)現(xiàn)客戶端,XAML不再貼出來,大家可以參考前面的幾篇文章,使用WebClient獲取數(shù)據(jù),返回的結(jié)果是一個XML文件:

private void UserControl_Loaded(object sender, RoutedEventArgs e){    Uri uri = new Uri("http://localhost:8081/BlogDataService.svc/Posts");    WebClient client = new WebClient();    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);    client.OpenReadAsync(uri);}void client_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e){    if (e.Error == null)    {    }}

我們可以使用LINQ to XML進(jìn)行數(shù)據(jù)的讀取,在Silverlight項(xiàng)目中建立一個Post類,跟上面的Post類一樣,然后使用LINQ to XML讀取:

XmlReader reader = XmlReader.Create(e.Result);XDocument postdoc = XDocument.Load(reader);XNamespace xmlns = "http://www.w3.org/2005/Atom";XNamespace ads = "http://schemas.microsoft.com/ado/2007/08/dataweb";var posts = from x in postdoc.Descendants(xmlns + "entry")            select new Post            {                Id = int.Parse(x.Descendants(ads + "Id").First().Value),                Title = x.Descendants(ads + "Title").First().Value,                Author = x.Descendants(ads + "Author").First().Value            };Posts.ItemsSource = posts;

完成的代碼如下所示:

private void UserControl_Loaded(object sender, RoutedEventArgs e){    Uri uri = new Uri("http://localhost:8081/BlogDataService.svc/Posts");    WebClient client = new WebClient();    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);    client.OpenReadAsync(uri);}void client_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e){    if (e.Error == null)    {        XmlReader reader = XmlReader.Create(e.Result);        XDocument postdoc = XDocument.Load(reader);        XNamespace xmlns = "http://www.w3.org/2005/Atom";        XNamespace ads = "http://schemas.microsoft.com/ado/2007/08/dataweb";        var posts = from x in postdoc.Descendants(xmlns + "entry")                    select new Post                    {                        Id = int.Parse(x.Descendants(ads + "Id").First().Value),                        Title = x.Descendants(ads + "Title").First().Value,                        Author = x.Descendants(ads + "Author").First().Value                    };        Posts.ItemsSource = posts;    }}

完整的示例就到這里了,運(yùn)行后的結(jié)果與前面的一樣。

TerryLee_Silverlight2_0065

結(jié)束語

本文簡單介紹了在Silverlight 2調(diào)用ADO.NET Data Services,由于對ADO.NET Data Services了解不多,有錯誤的地方還請大家斧正,你可以從這里下載示例代碼。

NET技術(shù)一步一步學(xué)Silverlight :數(shù)據(jù)與通信之ADO.NET Data Services,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 精品中文字幕乱码一区二区 | 六月婷婷在线视频 | 护士freesex欧美 | 国产免费美女 | 日韩激情在线播放 | 欧美成人天天综合在线视色 | 风间由美一区二区播放合集 | 久久综合狠狠综合狠狠 | 久久精品国产屋 | 免费视频久久看 | 午夜视频www | 99久久精品免费看国产一区二区 | 国产第一导航深夜福利 | 狠狠ady精品| 84pao强力永久免费高清 | 久久精品*5在热 | 色婷婷综合激情 | 激情婷婷 | 国产吧在线视频 | 欧美另类极品videosbest视 | 在线亚洲观看 | 日韩中文在线播放 | 亚洲精品9999久久久久 | 午夜国产情侣拍视频 | 永久免费在线观看视频 | 桃花综合久久久久久久久久网 | 久久精品国产主播一区二区 | 久久综合亚洲伊人色 | 精品欧美一区二区三区四区 | 亚洲欧洲日产国码 最新 | 91福利免费体验区观看区 | 99国产成人高清在线视频 | 免费黄色在线观看 | 欧美一区二区三区综合色视频 | 激情福利网站 | 火辣福利在线观看 | 色女人影院 | 伊人精品在线 | 国产精品福利在线观看入口 | 久久综合久久88 | 欧美人与禽|