第15课 数据与通信之ASMX

第15课 数据与通信之ASMX,第1张

概述概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章将从Silverlight 2基础知识、数据 概述

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",new Post{ ID=3,Title="一步一步学Silverlight 2系列(11):数据绑定",new Post{ ID=4,Title="一步一步学Silverlight 2系列(10):使用用户控件",new Post{ ID=5,Title="一步一步学Silverlight 2系列(9):使用控件模板",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,你可以从这里下载示例代码。

总结

以上是内存溢出为你收集整理的第15课 数据与通信之ASMX全部内容,希望文章能够帮你解决第15课 数据与通信之ASMX所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1044390.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存