概述
Silverlight 2 Beta 1版本發佈了,無論從Runtime還是Tools都給我們帶來了很多的驚喜,如支持框架語言Visual Basic, Visual C#, IronRuby, Ironpython,對JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步學Silverlight 2系列》文章將從Silverlight 2基礎知識、數據與通信、自定義控件、動畫、圖形圖像等幾個方面帶您快速進入Silverlight 2開發。
本文將簡單介紹在Silverlight 2中如何調用ADO.NET Data Services。
準備知識
由於ADO.NET Data Services是在ASP.NET 3.5 Extensions中,所以在開始本文示例之前,首先要安裝一下ASP.NET 3.5 Extensions最新版本,你可以從這裏下載。安裝完成後,在添加新項對話框中應該能夠看到ADO.NET Data Service項:
ADO.NET Data Service允許應用程序把數據以服務的形式公開,這樣我們就可以通過瀏覽器來直接訪問數據,它支持開放的業界標準,如AtomPub和JSON。它支持標準的HTTP動作如POST、GET、PUT、DELETE,用來完成數據的創建、更新、刪除和讀取。ADO.NET Data Service的知識這裏不再多說,大家可以去查看相關的資料。
簡單示例
如果大家看了前面三篇文章的話,可能對於下面的這個界面已經很煩了,不過在本文我會仍然採用這個示例進行演示:)
建立完Silverlight 2項目之後,我們在Web項目中添加一個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]特性,最終完成後代碼應該如下:
public class Post { [DataWebKey] public int Id { get; set; } public string Title { get; set; } public string Author { get; set; } }
public class Blog { public Blog() { _post.Add(new Post { Id = 1, Title = "一步一步學Silverlight 2系列(13):數據與通信之WebRequest", Author = "TerryLee" }); _post.Add(new Post { Id = 2, Title = "一步一步學Silverlight 2系列(12):數據與通信之WebClient", Author = "TerryLee" }); _post.Add(new Post { Id = 3, Title = "一步一步學Silverlight 2系列(11):數據綁定", Author = "TerryLee" }); _post.Add(new Post { Id = 4, Title = "一步一步學Silverlight 2系列(10):使用用戶控件", Author = "TerryLee" }); _post.Add(new Post { Id = 5, Title = "一步一步學Silverlight 2系列(9):使用控件模板", Author = "TerryLee" }); _post.Add(new Post { Id = 6, Title = "一步一步學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:
實現服務,讓它繼承於泛型的WebDataService,並且設置訪問權限。
public class BlogDataService : WebDataService<Blog> { public static void InitializeService(IWebDataServiceConfiguration config) { config.SetResourceContainerAcce***ule("*", ResourceContainerRights.AllRead); } }
現在我們的服務端就完成了,現在我們可以在瀏覽器中訪問BlogDataService.svc,應該可以看到如下界面:
現在還看不到所有的Posts,我們可以在地址欄中輸入[url]http://localhost:8081/BlogDataService.svc/Posts[/url],瀏覽器會默認爲Feed打開,可以查看源代碼,將會看到所有內容,XML內容如下(只列出片段):
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <feed xml:base="[url]http://localhost:8081/BlogDataService.svc/[/url]" ......> <id>[url]http://localhost:8081/BlogDataService.svc/Posts[/url]</id> <updated /> <title>Posts</title> <link rel="self" href="Posts" title="Posts" /> <entry adsm:type="TerryLee.SilverlightWithDataServiceDemoWeb.Post"> <id>[url]http://localhost:8081/BlogDataService.svc/Posts[/url](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>一步一步學Silverlight 2系列(13):數據與通信之WebRequest</ads:Title> <ads:Author>TerryLee</ads:Author> </content> </entry>
當然還可以進行其他的查詢,使用filter和orderby等,如[url]http://localhost:8081/BlogDataService.svc/Posts?$filter=Id[/url] eq 1&$orderby=Id,這裏不在介紹。至此我們的數據服務端就算完成了。下面再實現客戶端,XAML不再貼出來,大家可以參考前面的幾篇文章,使用WebClient獲取數據,返回的結果是一個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進行數據的讀取,在Silverlight項目中建立一個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; } }
完整的示例就到這裏了,運行後的結果與前面的一樣。
結束語
本文簡單介紹了在Silverlight 2調用ADO.NET Data Services,由於對ADO.NET Data Services瞭解不多,有錯誤的地方還請大家斧正,你可以從這裏下載示例代碼。
本文出自 “TerryLee技術專欄” 博客,請務必保留此出處[url]http://terrylee.blog.51cto.com/342737/67255[/url]
本文出自 51CTO.COM技術博客