一步一步學Silverlight 2系列(15):數據與通信之ASMX

概述

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中如何與ASMX進行通信。

簡單示例

本文的示例非常簡單,其過程也跟我們在一步一步學Silverlight 2系列(14):數據與通信之WCF中差不多,我們仍然顯示一個最新隨筆的列表,最終完成後效果如下所示:
 
定義一個業務實體Post。
public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
}
在Web項目中添加一個Web Service文件,命名爲BlogService.asmx
 
實現該服務,定義一個GetPosts方法:
public class BlogService : WebService
{
    [WebMethod]
    public Post[] GetPosts()
    {
        List<Post> posts = new List<Post>()
        {
            new Post{ Id=1, Title="一步一步學Silverlight 2系列(13):數據與通信之WebRequest", Author="TerryLee" },
            new Post{ Id=2, Title="一步一步學Silverlight 2系列(12):數據與通信之WebClient", Author="TerryLee" },
            new Post{ Id=3, Title="一步一步學Silverlight 2系列(11):數據綁定", Author="TerryLee" },
            new Post{ Id=4, Title="一步一步學Silverlight 2系列(10):使用用戶控件", Author="TerryLee" },
            new Post{ Id=5, Title="一步一步學Silverlight 2系列(9):使用控件模板", Author="TerryLee" },
            new Post{ Id=6, Title="一步一步學Silverlight 2系列(8):使用樣式封裝控件觀感", Author="TerryLee" }
        };
        return posts.ToArray();
    }
}
同樣設置Web Development Server的端口號爲一個固定值,這裏設爲8081,然後在瀏覽器中測試服務是否正確:
 
點擊調用後測試服務正確
 
在Silverlight項目中,添加對服務引用,
 
使用對象瀏覽器查看一下生成客戶端代理類中的對象:
 
編寫展示界面,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>
實現調用ASMX,並進行數據的綁定。仍然採用異步模式,所使用的方法如上圖中紅色框中的部分。過程與WCF通信差不多,只不過不再需要指定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;
        }
    }
}
一個完整的Silverlight 2中調用ASMX的示例就完成了,運行後效果如下:
 

結束語

本文簡單介紹了在Silverlight 2中如何調用ASMX,你可以從這裏下載示例代碼。
本文出自 “TerryLee技術專欄” 博客,請務必保留此出處[url]http://terrylee.blog.51cto.com/342737/67252[/url]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章