vs2010 学习Silverlight学习笔记(14):数据与通信之ASMX

概要:

  上午自己重写了WCF的试验,前前后后花了1个多小时。又把以前的关于数据通信的例子重做一遍。遇到了一些问题,都是因为操作不熟练造成的。
所以我希望和我一样的新手不论学什么,都要多动手,多复习。今天学习的是asmx,其实就是WebService。粗略的看了一下李老师的教程,觉得跟其他
的数据通信有很多相似的地方。所以我先试着自己动手写写看。

自己的例子:

  首先在web文件夹添加WebService,命名为MyService.asmx,这是用于 XML Web Services 的 ASP.NET 文件。
  
代码
public class MyService : System.Web.Services.WebService
{

[WebMethod]
public Student[] GetStudnet()
{
List
<Student> student = new List<Student>()
{
new Student (1,"",""),
new Student (2,"",""),
new Student (3,"",""),
new Student (4,"","")
};
return student.ToArray();
}
}

student是我自定义的一个类:

代码

public class Student
{
public Student(int no, string name, string sex)
{
this.NO = no;
this.Name = name;
this.Sex = sex;
}
public Student()
{ }

public int NO;

public string Name;

public string Sex;
}

这个不像WCF需要改web.config,只要将web的端口设一个固定值,运行:http://localhost:3333/MyService.asmx

  这个说明这个WebService可以运行了,点击GetStudent,会显示关于此web服务的xaml文件,我们就可以根据此xaml文件编写关于使用
此服务的程序了。继续点击,就会显示返回的Xaml文件:

代码

<?xml version="1.0" encoding="utf-8" ?>
-
<ArrayOfStudent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
-
<Student>
<NO>1</NO>
<Name></Name>
<Sex></Sex>
</Student>
-
<Student>
<NO>2</NO>
<Name></Name>
<Sex></Sex>
</Student>
-
<Student>
<NO>3</NO>
<Name></Name>
<Sex></Sex>
</Student>
-
<Student>
<NO>4</NO>
<Name></Name>
<Sex></Sex>
</Student>
</ArrayOfStudent>

这个就是当我们使用此服务的时候,这个服务所提供的返回数据。
MainPage.xaml
代码

<Grid x:Name="LayoutRoot" Background="#46461F">
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Grid.Row="0" Grid.Column="0" CornerRadius="15"
Background
="OldLace" Width="120" Height="35"
HorizontalAlignment
="Left" Margin="10 0 0 0">
<TextBlock Text="学生列表:" Foreground="Green" HorizontalAlignment="Left"
Margin
="10 0 0 0" FontSize="20"></TextBlock>
</Border>
<ListBox x:Name="student" Grid.Row="1" Grid.Column="0"
Margin
="30 0 0 0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding NO}" Height="30" Width="10"></TextBlock>
<TextBlock Text="{Binding Name}" Height="30" Width="10"></TextBlock>
<TextBlock Text="{Binding Sex}" Height="30" Width="10"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>

在这之前要将此Service引入到Silverlight中,

MainPage.xaml.cs

代码

//引入
using SilverlightAppDemo2.MyStudentService;

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MyServiceSoapClient myClient
= new MyServiceSoapClient();
myClient.GetStudnetCompleted
+= new EventHandler<GetStudnetCompletedEventArgs>(client_GetPostsCompleted);
myClient.GetStudnetAsync();
}
void client_GetPostsCompleted(object sender, GetStudnetCompletedEventArgs e)
{
if (e.Error == null)
{
student.ItemsSource
= e.Result;
}
}

运行一下:

类序列化问题:

  这个是我对照TerryLee的例子:
他的实体类:
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
}

赋值:

代码

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" }
};

我的实体类:

代码

public Student(int no, string name, string sex)
{
this.NO = no;
this.Name = name;
this.Sex = sex;
}
//public Student()
//{ }
public int NO;
public string Name;
public string Sex;

当我的代码中没有public Student(){}时,服务就会出错,提示实体类不能序列化。
SilverlightAppDemo2.Web.Student cannot be serialized because it does not have a parameterless constructor.
这个让我想起了上一篇中WCF序列化的问题,
[DataContract]    [DataMember]
当时还查询了资料,后来知道这个是WCF中类序列化用的。在Silverlight2中就可以省略不写了,但做一些特殊的用法时,还是要加上的。

当我再将public Student(){}加上时就能序列化了。如果不加此无参构造函数,即使加[Serializable]也没用。
查了一下,发现是这样的:
首先Serializable是完全序列化,并且记录字段值在构造器中的修改,在反序列化时,先将该类的字段初始化,
然后将序列化流中所得数据分配给字段。如果一个类实现了Serializable,但他的父类并不是可序列化的,
那么该父类必须要有个无参构造函数。


总结:

  虽然前几个学习很吃力,但是经过我多动手,慢慢也会发现一些相通的地方。今天很高兴,因为很多都是自己动手做的。但我还是
希望多碰到新的问题,解决新的问题,学习新的问题。
总目录
上一篇:vs2010 学习Silverlight学习笔记(13):数据与通信之WCF

 



posted @ 2010-05-19 20:25  耀哥  阅读(1434)  评论(0编辑  收藏  举报