|
概述
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; }}
再添加一個Blog類,它有一個返回類型為IQueryable<Post>的屬性Posts:
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.SetResourceContainerAccessRule("*", ResourceContainerRights.AllRead); }}
現在我們的服務端就完成了,現在我們可以在瀏覽器中訪問BlogDataService.svc,應該可以看到如下界面:
現在還看不到所有的Posts,我們可以在地址欄中輸入http://localhost:8081/BlogDataService.svc/Posts,瀏覽器會默認為Feed打開,可以查看源代碼,將會看到所有內容,XML內容如下(只列出片段):
<?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>一步一步學Silverlight 2系列(13):數據與通信之WebRequest</ads:Title> <ads:Author>TerryLee</ads:Author> </content> </entry>
如果要查看某一條文章的內容,可以輸入http://localhost:8081/BlogDataService.svc/Posts(2)進行查看,如下圖所示。
當然還可以進行其他的查詢,使用filter和orderby等,如http://localhost:8081/BlogDataService.svc/Posts?$filter=Id 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了解不多,有錯誤的地方還請大家斧正,你可以從這里下載示例代碼。
NET技術:一步一步學Silverlight :數據與通信之ADO.NET Data Services,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。