|
概述
Silverlight 2 Beta 1版本發(fā)布了,無(wú)論從Runtime還是Tools都給我們帶來(lái)了很多的驚喜,如支持框架語(yǔ)言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開(kāi)發(fā)。
本文將簡(jiǎn)單介紹在Silverlight 2中如何與ASMX進(jìn)行通信。
簡(jiǎn)單示例
本文的示例非常簡(jiǎn)單,其過(guò)程也跟我們?cè)?a >一步一步學(xué)Silverlight 2系列(14):數(shù)據(jù)與通信之WCF中差不多,我們?nèi)匀伙@示一個(gè)最新隨筆的列表,最終完成后效果如下所示:
定義一個(gè)業(yè)務(wù)實(shí)體Post。
public class Post{ public int Id { get; set; } public string Title { get; set; } public string Author { get; set; }}
在Web項(xiàng)目中添加一個(gè)Web Service文件,命名為BlogService.asmx
實(shí)現(xiàn)該服務(wù),定義一個(gè)GetPosts方法:
public class BlogService : WebService{ [WebMethod] public Post[] GetPosts() { List<Post> posts = new List<Post>() { new Post{ Id=1, Title="一步一步學(xué)Silverlight 2系列(13):數(shù)據(jù)與通信之WebRequest", Author="TerryLee" }, new Post{ Id=2, Title="一步一步學(xué)Silverlight 2系列(12):數(shù)據(jù)與通信之WebClient", Author="TerryLee" }, new Post{ Id=3, Title="一步一步學(xué)Silverlight 2系列(11):數(shù)據(jù)綁定", Author="TerryLee" }, new Post{ Id=4, Title="一步一步學(xué)Silverlight 2系列(10):使用用戶控件", Author="TerryLee" }, new Post{ Id=5, Title="一步一步學(xué)Silverlight 2系列(9):使用控件模板", Author="TerryLee" }, new Post{ Id=6, Title="一步一步學(xué)Silverlight 2系列(8):使用樣式封裝控件觀感", Author="TerryLee" } }; return posts.ToArray(); }}
同樣設(shè)置Web Development Server的端口號(hào)為一個(gè)固定值,這里設(shè)為8081,然后在瀏覽器中測(cè)試服務(wù)是否正確:
點(diǎn)擊調(diào)用后測(cè)試服務(wù)正確
在Silverlight項(xiàng)目中,添加對(duì)服務(wù)引用,
使用對(duì)象瀏覽器查看一下生成客戶端代理類中的對(duì)象:
編寫展示界面,XAML如下,與上一篇中的示例一樣:
<Grid Background="#46461F"> <Grid.RowDefinitions> <RowDefinition Height="40"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <Border Grid.Row="0" Grid.Column="0" CornerRadius="15" Width="240" Height="36" Background="Orange" Margin="20 0 0 0" HorizontalAlignment="Left"> <TextBlock Text="最新隨筆" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20 0 0 0"></TextBlock> </Border> <ListBox x:Name="Posts" Grid.Row="1" Margin="40 10 10 10"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Id}" Height="40" Foreground="Red"></TextBlock> <TextBlock Text="{Binding Title}" Height="40"></TextBlock> <TextBlock Text="{Binding Author}" Height="40" Foreground="Orange"></TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox></Grid>
實(shí)現(xiàn)調(diào)用ASMX,并進(jìn)行數(shù)據(jù)的綁定。仍然采用異步模式,所使用的方法如上圖中紅色框中的部分。過(guò)程與WCF通信差不多,只不過(guò)不再需要指定Bingding等信息:
public partial class Page : UserControl{ public Page() { InitializeComponent(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { BlogServiceSoapClient client = new BlogServiceSoapClient(); client.GetPostsCompleted += new EventHandler<GetPostsCompletedEventArgs>(client_GetPostsCompleted); client.GetPostsAsync(); } void client_GetPostsCompleted(object sender, GetPostsCompletedEventArgs e) { if (e.Error == null) { Posts.ItemsSource = e.Result; } }}
一個(gè)完整的Silverlight 2中調(diào)用ASMX的示例就完成了,運(yùn)行后效果如下:
結(jié)束語(yǔ)
本文簡(jiǎn)單介紹了在Silverlight 2中如何調(diào)用ASMX,你可以從這里下載示例代碼。
NET技術(shù):一步一步學(xué)Silverlight :數(shù)據(jù)與通信之ASMX,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。