|
概述
Silverlight 2 Beta 1版本發(fā)布了,無論從Runtime還是Tools都給我們帶來了很多的驚喜,如支持框架語言Visual Basic, Visual C#, IronRuby, IronPython,對(duì)JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步學(xué)Silverlight 2系列》文章將從Silverlight 2基礎(chǔ)知識(shí)、數(shù)據(jù)與通信、自定義控件、動(dòng)畫、圖形圖像等幾個(gè)方面帶您快速進(jìn)入Silverlight 2開發(fā)。
本文將簡單介紹在Silverlight 2中如何調(diào)用ADO.NET Data Services。
準(zhǔn)備知識(shí)
由于ADO.NET Data Services是在ASP.NET 3.5 Extensions中,所以在開始本文示例之前,首先要安裝一下ASP.NET 3.5 Extensions最新版本,你可以從這里下載。安裝完成后,在添加新項(xiàng)對(duì)話框中應(yīng)該能夠看到ADO.NET Data Service項(xiàng):
ADO.NET Data Service允許應(yīng)用程序把數(shù)據(jù)以服務(wù)的形式公開,這樣我們就可以通過瀏覽器來直接訪問數(shù)據(jù),它支持開放的業(yè)界標(biāo)準(zhǔn),如AtomPub和JSON。它支持標(biāo)準(zhǔn)的HTTP動(dòng)作如POST、GET、PUT、DELETE,用來完成數(shù)據(jù)的創(chuàng)建、更新、刪除和讀取。ADO.NET Data Service的知識(shí)這里不再多說,大家可以去查看相關(guān)的資料。
簡單示例
如果大家看了前面三篇文章的話,可能對(duì)于下面的這個(gè)界面已經(jīng)很煩了,不過在本文我會(huì)仍然采用這個(gè)示例進(jìn)行演示:)
建立完Silverlight 2項(xiàng)目之后,我們?cè)赪eb項(xiàng)目中添加一個(gè)Post類:
public class Post{ public int Id { get; set; } public string Title { get; set; } public string Author { get; set; }}
我們用Id作為Post的主鍵,這里需要添加對(duì)于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; }}
再添加一個(gè)Blog類,它有一個(gè)返回類型為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>(); } }}
添加一個(gè)ADO.NET Data Service,取名BlogDataService.svc:
實(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)該可以看到如下界面:
現(xiàn)在還看不到所有的Posts,我們可以在地址欄中輸入http://localhost:8081/BlogDataService.svc/Posts,瀏覽器會(huì)默認(rèn)為Feed打開,可以查看源代碼,將會(huì)看到所有內(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)行查看,如下圖所示。
當(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é)果是一個(gè)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)目中建立一個(gè)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é)果與前面的一樣。
結(jié)束語
本文簡單介紹了在Silverlight 2調(diào)用ADO.NET Data Services,由于對(duì)ADO.NET Data Services了解不多,有錯(cuò)誤的地方還請(qǐng)大家斧正,你可以從這里下載示例代碼。
NET技術(shù):一步一步學(xué)Silverlight :數(shù)據(jù)與通信之ADO.NET Data Services,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。