本文我们学习如何在Silverlight中使用WebService进行通讯。
新建项目Silverlight应用程序,命名为:SLWebService。
在服务器端我们需要做两项目工作:
1、在Web项目中新建一个类Person,我们将在WebService中返回它的实例化对象。Person类定义如下:
using System;
using System.Collections.Generic;
using System.linq;
using System.Web;
namespace SLWebService.Web
{
public class Person
{
public string name { get ; set ; }
public int Age { get ; set ; }
}
}
2、在Web项目中建立一个WebService,命名为MySLWebService.asmx,它的主要任务就是返回一个Person类数组,代码如下:
1、建立用户界面.Page.xaml代码如下:
Code
using System;
using System.Collections.Generic;
using System.linq;
using System.Web;
using System.Web.Services;
namespace SLWebService.Web
{
/// <summary>
/// MySLWebService 的摘要说明
/// </summary>
[WebService(namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolBoxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class MySLWebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public Person[] GetPeople()
{
List<Person> People = new List<Person>()
{
new Person{ name="Jack",Age=12},
new Person{ name="Tom",Age=22},
new Person{ name="Simon",Age=32},
new Person{ name="Richard",Age=26}
};
return People.ToArray();
}
}
}
在客户端我们需要做如下工作:
< UserControl x:Class = " SLWebService.Page "
xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "
WIDth = " 400 " Height = " 300 " >
< StackPanel WIDth = " 400 " Height = " 300 " Background = " Wheat " >
< TextBlock Text = " 通过WebService取得的数据如下 " TextAlignment = " Center " Foreground = " Red " FontSize = " 18 " ></ TextBlock >
< button x:name = " btnGetWebService " WIDth = " 200 " Height = " 30 " Content = " 获取数据 " Click = " btnGetWebService_Click " ></ button >
< ListBox x:name = " People " WIDth = " 300 " Height = " 200 " @R_301_5553@ = " 20 " >
< ListBox.ItemTemplate >
< DataTemplate >
< StackPanel OrIEntation = " Vertical " >
< StackPanel OrIEntation = " Horizontal " >
< TextBlock Text = " 姓名 " WIDth = " 100 " Foreground = " Blue " ></ TextBlock >
< TextBlock Text = " 年龄 " WIDth = " 100 " Foreground = " DarkBlue " ></ TextBlock >
</ StackPanel >
< StackPanel OrIEntation = " Horizontal " >
< TextBlock Text = " {Binding name} " Foreground = " Red " WIDth = " 100 " ></ TextBlock >
< TextBlock Text = " {Binding Age} " Foreground = " Green " WIDth = " 100 " ></ TextBlock >
</ StackPanel >
</ StackPanel >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >
</ StackPanel >
</ UserControl >
界面如下:
2、在Silverlight项目中引用服务器端的WebService,命名为MyWebServiceRef。
引用后,程序如下图:
3、在客户端使用WebService,通过WebService从服务器端取得数据,在本地处理后显示在用房界面上。Page.xaml.cs代码如下:
using System.Collections.Generic;
using System.linq;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using SLWebService.MyWebServiceRef; // 加入对MyWebServiceRef的引用
namespace SLWebService
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private voID btnGetWebService_Click( object sender,RoutedEventArgs e)
{
// 使用WebService从服务器端得到数据并在本地端进行处理
MySLWebServiceSoapClIEnt clIEnt = new MySLWebServiceSoapClIEnt();
clIEnt.GetPeopleCompleted += new EventHandler < GetPeopleCompletedEventArgs > (clIEnt_GetPeopleCompleted);
clIEnt.GetPeopleAsync();
}
voID clIEnt_GetPeopleCompleted( object sender,GetPeopleCompletedEventArgs e)
{
if (e.Error == null )
{
People.ItemsSource = e.Result; // 绑定结果到UI的List控件
}
}
}
}
效果如下图:
前往:Silverlight学习笔记清单
本文程序在Silverlight2.0和VS2008环境中调试通过。本文参照了部分网络资料,希望能够抛砖引玉,大家共同学习。
http://www.cnblogs.com/wsdj-ITtech/archive/2009/08/28/1555525.html
总结以上是内存溢出为你收集整理的SilverLight学习笔记--Silverlight中WebService通讯全部内容,希望文章能够帮你解决SilverLight学习笔记--Silverlight中WebService通讯所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)