日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SilverLight学习笔记--Silverlight中WebService通讯

發布時間:2023/12/1 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SilverLight学习笔记--Silverlight中WebService通讯 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文我們學習如何在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類數組,代碼如下:
?

?

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();
????????}
????????
????}
}

在客戶端我們需要做如下工作:
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取得的數據如下"?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"?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項目中引用服務器端的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?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環境中調試通過。本文參照了部分網絡資料,希望能夠拋磚引玉,大家共同學習。
(轉載本文請注明出處)

轉載于:https://www.cnblogs.com/wsdj-ITtech/archive/2009/08/28/1555525.html

總結

以上是生活随笔為你收集整理的SilverLight学习笔记--Silverlight中WebService通讯的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。