SilverLight学习笔记--Silverlight中WebService通讯
本文我們學(xué)習(xí)如何在Silverlight中使用WebService進(jìn)行通訊。
新建項目Silverlight應(yīng)用程序,命名為:SLWebService。
在服務(wù)器端我們需要做兩項目工作:
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,它的主要任務(wù)就是返回一個Person類數(shù)組,代碼如下:
?
?
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?從腳本中調(diào)用此?Web?服務(wù),請取消對下行的注釋。
????//?[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();
????????}
????????
????}
}
在客戶端我們需要做如下工作:
1、建立用戶界面.Page.xaml代碼如下:
?
<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取得的數(shù)據(jù)如下"?TextAlignment="Center"?Foreground="Red"??FontSize="18"></TextBlock>
?????????<Button?x:Name="btnGetWebService"?Width="200"?Height="30"?Content="獲取數(shù)據(jù)"?Click="btnGetWebService_Click"></Button>
????????<ListBox?x:Name="People"?Width="300"?Height="200"?Margin="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項目中引用服務(wù)器端的WebService,命名為MyWebServiceRef。
????????????????????????
引用后,程序如下圖:
????????????????????????
3、在客戶端使用WebService,通過WebService從服務(wù)器端取得數(shù)據(jù),在本地處理后顯示在用房界面上。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從服務(wù)器端得到數(shù)據(jù)并在本地端進(jìn)行處理
????????????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;?//綁定結(jié)果到UI的List控件
????????????}
????????}
????}
}
效果如下圖:
????????????????????????
前往:Silverlight學(xué)習(xí)筆記清單
(轉(zhuǎn)載本文請注明出處)
轉(zhuǎn)載于:https://www.cnblogs.com/wsdj-ITtech/archive/2009/08/28/1555525.html
總結(jié)
以上是生活随笔為你收集整理的SilverLight学习笔记--Silverlight中WebService通讯的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西替利嗪多少钱啊?
- 下一篇: 输卵管结扎复通成功率大不大?