- 浏览: 1490997 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (525)
- SEO (16)
- JAVA-EE-Hibernate (6)
- JAVA-EE-Struts (29)
- JAVA-EE-Spring (15)
- Linux (37)
- JAVA-SE (29)
- NetWork (1)
- CMS (14)
- Semantic Research (3)
- RIA-Flex (0)
- Ajax-Extjs (4)
- Ajax-Jquery (1)
- www.godaddy.com (0)
- SSH (34)
- JavaScript (6)
- SoftwareEngineer (9)
- CMMI (0)
- IDE-Myeclipse (3)
- PHP (1)
- Algorithm (3)
- C/C++ (18)
- Concept&Items (2)
- Useful WebSite (1)
- ApacheServer (2)
- CodeReading (1)
- Socket (2)
- UML (10)
- PowerDesigner (1)
- Repository (19)
- MySQL (3)
- SqlServer (0)
- Society (1)
- Tomcat (7)
- WebService (5)
- JBoss (1)
- FCKeditor (1)
- PS/DW/CD/FW (0)
- DesignPattern (11)
- WebSite_Security (1)
- WordPress (5)
- WebConstruction (3)
- XML|XSD (7)
- Android (0)
- Project-In-Action (9)
- DatabaseDesign (3)
- taglib (7)
- DIV+CSS (10)
- Silverlight (52)
- JSON (7)
- VC++ (8)
- C# (8)
- LINQ (1)
- WCF&SOA (5)
- .NET (20)
- SOA (1)
- Mashup (2)
- RegEx (6)
- Psychology (5)
- Stock (1)
- Google (2)
- Interview (4)
- HTML5 (1)
- Marketing (4)
- Vaadin (2)
- Agile (2)
- Apache-common (6)
- ANTLR (0)
- REST (1)
- HtmlAnalysis (18)
- csv-export (3)
- Nucth (3)
- Xpath (1)
- Velocity (6)
- ASP.NET (9)
- Product (2)
- CSS (1)
最新评论
-
lt26w:
理解成门面模式应该比较容易明白吧
FacadePattern-Java代码实例讲解 -
lt26w:
看下面的例子比较明白.
FacadePattern-Java代码实例讲解 -
javaloverkehui:
这也叫文档,别逗我行吗,也就自己看看。
HtmlCleaner API -
SE_XiaoFeng:
至少也应该写个注释吧。
HtmlCleaner API -
jfzshandong:
...
org.springframework.web.filter.CharacterEncodingFilter 配置
本文我们学习如何在Silverlight中使用WebService进行通讯。
新建项目Silverlight应用程序,命名为:SLWebService。
在服务器端我们需要做两项目工作:
1、在Web项目中新建一个类Person,我们将在WebService中返回它的实例化对象。Person类定义如下:
<!-- <br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->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 ; }
}
}
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
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->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();
}
}
}
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->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代码如下:
<!-- <br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><
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 >
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代码如下:
<!-- <br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->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控件
}
}
}
}
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环境中调试通过。本文参照了部分网络资料,希望能够抛砖引玉,大家共同学习。
(转载本文请注明出处)
Tag标签: Silverlight
,WebService
发表评论
-
SilverLight异步调用WebService出错!
2010-01-19 12:58 5366SilverLight异步调用WebService出错! ... -
Silverlight播放器 C#语言
2010-01-13 13:30 3260这段时间研究Silverlight中的MediaElement ... -
使用Silverlight,制作简单播放器的一点点心得。
2010-01-13 13:28 3616首先介绍什么是Silverligh ... -
初探silverlight--简易播放器
2010-01-13 13:28 1477<UserControl xmlns=" ... -
新开发的silverlight视频播放器,
2010-01-13 13:21 4440http://www.chenjiliang.com/Arti ... -
Silverlight教程第四部分:使用 Style 元素更好地封装观感
2010-01-12 22:11 1226Silverlight教程第四部分 ... -
Silverlight Carousel: Creating a Silverlight Control Displays Picture in an Inte
2010-01-12 18:18 1828http://www.codeproject.com/KB/s ... -
Using projection to build a 3D carousel in Silverlight 3
2010-01-12 18:14 2288http://ww ... -
CoverFlow – built using Silverlight 3's 'Projection' feature
2010-01-12 18:11 1846CoverFlow – built using Silver ... -
silverlight动画播放停止重播等控制
2010-01-06 12:38 1353ani.begin() ani.stop(); ani. ... -
silverlight速学范例100
2010-01-06 12:37 1243silverlight速学范例100 ... -
Silverlight 中的 HTTP 通信和安全
2010-01-04 23:43 1979Silverlight 中的 HTTP 通信和安全 < ... -
Visual Studio的 诡异bug(mscorlib无法引用)引发的对话 and Silverlight XAML 构造出错
2010-01-04 09:25 4079... -
Silverlight常见问题及解决方法
2009-12-22 14:06 1229Silverlight常见问题及解决方法 ... -
网上常用免费webservice 查询
2009-12-22 12:47 4556网上常用免费webservice 查询 2008-11 ... -
必应 Bing 新特性之最新文章, Wolfram|Alpha 整合, 天气搜索等已推出
2009-12-21 23:33 1464必应 Bing 增加了一项“最新文章”的搜索结果特性,例如下图 ... -
下载silverlight官网的全部视频教程
2009-12-21 23:30 14813Silverlight官网提供了许 ... -
Silverlight客户端和WCF服务器端共享类库
2009-12-21 23:21 1793在Silverlight企业级项目开发中,访问数据库是很常见的 ... -
Create a Silverlight Europe weather map
2009-12-21 22:55 1546I don’t generally fi ... -
必应地图图片系统(Tile System)之二
2009-12-21 22:53 2248【坐标系和地图图片编 ...
相关推荐
silverlight-通过-WebService-连接数据库.doc
包括服务端配置工程和客户端配置工程。 是我这个周末的学习总结.. 已测试可运行。
122158-Webservice学习笔记.doc
dubbo-rpc-webservice-2.8.4 dubbo-rpc-webservice-2.8.4
webService学习笔记
Axis2--Webservice Axis2--Webservice Axis2--Webservice
SilverLight 2.0 调用 WebService 视频教程 SilverLight 2.0 调用 WebService 视频教程 SilverLight 2.0 调用 WebService 视频教程 SilverLight 2.0 调用 WebService 视频教程
Laravel开发-laravel-webservice 基于httpful的Laravel Web服务提供商
WebService学习笔记 比较经典
webservice学习笔记doc文档,喜欢或想要了解webservice的朋友可以下载学习一下。
axis2-webservice axis2-webservice axis2-webservice axis2-webservice axis2-webservice
尚硅谷Webservice学习笔记,详情请参看文档,仅供个人学习
osgi-webservice教程 教授你如果通过osgi架构开发java的webservice
Webservice学习笔记
SOA -Webservice SOA -Webservice SOA -Webservice SOA -Webservice
OracleEBS-WebService开发笔记.pdf
JavaEE5学习笔记09-将EJB发布成WebService总结。
Laravel开发-webservice 请求到其他服务器的包
SAP与其他外围系统交互使用的一种通讯方式,具体讲述了WSDL文件的发布与在SAP中的部署,以及接口的测试和配置方式