哈尔滨的网站建设公司,石景山做网站的公司,网站建设费 广告,两学一做注册网站吗本文我们学习如何在Silverlight中使用WebService进行通讯。 新建项目Silverlight应用程序#xff0c;命名为#xff1a;SLWebService。在服务器端我们需要做两项目工作: 1、在Web项目中新建一个类Person#xff0c;我们将在WebService中返回它的实例化对象。Person类定义如下…本文我们学习如何在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类数组,代码如下: Codeusing 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() { ListPerson People new ListPerson() { new Person{ NameJack,Age12}, new Person{ NameTom,Age22}, new Person{ NameSimon,Age32}, new Person{ NameRichard,Age26} }; return People.ToArray(); } } } 在客户端我们需要做如下工作: 1、建立用户界面.Page.xaml代码如下: UserControl x:ClassSLWebService.Page xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml Width400 Height300 StackPanel Width400 Height300 BackgroundWheat TextBlock Text通过WebService取得的数据如下 TextAlignmentCenter ForegroundRed FontSize18/TextBlock Button x:NamebtnGetWebService Width200 Height30 Content获取数据 ClickbtnGetWebService_Click/Button ListBox x:NamePeople Width300 Height200 Margin20 ListBox.ItemTemplate DataTemplate StackPanel OrientationVertical StackPanel OrientationHorizontal TextBlock Text姓名 Width100 ForegroundBlue /TextBlock TextBlock Text年龄 Width100 ForegroundDarkBlue/TextBlock /StackPanel StackPanel OrientationHorizontal TextBlock Text{Binding Name} ForegroundRed Width100 /TextBlock TextBlock Text{Binding Age} ForegroundGreen Width100 /TextBlock /StackPanel /StackPanel /DataTemplate /ListBox.ItemTemplate /ListBox /StackPanel/UserControl 界面如下: 2、在Silverlight项目中引用服务器端的WebService命名为MyWebServiceRef。 引用后程序如下图: 3、在客户端使用WebService通过WebService从服务器端取得数据在本地处理后显示在用房界面上。Page.xaml.cs代码如下: using System;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 EventHandlerGetPeopleCompletedEventArgs(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环境中调试通过。本文参照了部分网络资料希望能够抛砖引玉大家共同学习。 (转载本文请注明出处)转载于:https://www.cnblogs.com/wsdj-ITtech/archive/2009/08/28/1555525.html