[索引页]
[源码下载]
作者:webabcd
介绍
Silverlight 3.0 通信的新增功能
在线DEMO
http://www.voidcn.com/article/p-boakcggz-tq.html
示例
1、以二进制 XML 传递数据的演示
服务端(WCF)
BinaryXmlService.svc
[源码下载]
稳扎稳打Silverlight(39) - 3.0通信之二进制XML通信,本地连接
作者:webabcd
介绍
Silverlight 3.0 通信的新增功能
- 二进制XML通信 - 与 WCF 服务间通信,可以使用二进制 XML 传递数据(提高传输性能)
- 本地连接 - 允许客户端的两个 Silverlight 程序之间直接进行通信(不用通过服务端)
在线DEMO
http://www.voidcn.com/article/p-boakcggz-tq.html
示例
1、以二进制 XML 传递数据的演示
服务端(WCF)
BinaryXmlService.svc
Web.config
<
system.serviceModel
>
< bindings >
< customBinding >
< binding name ="customBinding0" >
< binaryMessageEncoding />
< httpTransport />
</ binding >
</ customBinding >
</ bindings >
< serviceHostingEnvironment aspNetCompatibilityEnabled ="true" />
< behaviors >
< serviceBehaviors >
< behavior name ="Silverlight30.Service.BinaryXmlServiceBehavior" >
< serviceMetadata httpGetEnabled ="true" />
< serviceDebug includeExceptionDetailInFaults ="false" />
</ behavior >
</ serviceBehaviors >
</ behaviors >
< services >
< service behaviorConfiguration ="Silverlight30.Service.BinaryXmlServiceBehavior"
name ="Silverlight30.Service.BinaryXmlService" >
< endpoint address ="" binding="customBinding" bindingConfiguration ="customBinding0"
contract ="Silverlight30.Service.BinaryXmlService" />
< endpoint address ="mex" binding ="mexHttpBinding" contract ="IMetadataExchange" />
</ service >
</ services >
</ system.serviceModel >
< bindings >
< customBinding >
< binding name ="customBinding0" >
< binaryMessageEncoding />
< httpTransport />
</ binding >
</ customBinding >
</ bindings >
< serviceHostingEnvironment aspNetCompatibilityEnabled ="true" />
< behaviors >
< serviceBehaviors >
< behavior name ="Silverlight30.Service.BinaryXmlServiceBehavior" >
< serviceMetadata httpGetEnabled ="true" />
< serviceDebug includeExceptionDetailInFaults ="false" />
</ behavior >
</ serviceBehaviors >
</ behaviors >
< services >
< service behaviorConfiguration ="Silverlight30.Service.BinaryXmlServiceBehavior"
name ="Silverlight30.Service.BinaryXmlService" >
< endpoint address ="" binding="customBinding" bindingConfiguration ="customBinding0"
contract ="Silverlight30.Service.BinaryXmlService" />
< endpoint address ="mex" binding ="mexHttpBinding" contract ="IMetadataExchange" />
</ service >
</ services >
</ system.serviceModel >
客户端
BinaryXml.xaml
BinaryXml.xaml
<navigation:Page x:Class="Silverlight30.Communication.BinaryXml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="BinaryXml Page">
<Grid x:Name="LayoutRoot">
<StackPanel Orientation="Horizontal" Height="30">
<!--支持二进制 XML 通信-->
<TextBox x:Name="txtName" Text="webabcd" />
<Button x:Name="btnHelloConfig" Content="引用服务后(使用代理),通过配置的方式与服务端做二进制XML通信" Click="btnHelloConfig_Click" />
<Button x:Name="btnHelloCoding" Content="引用服务后(使用代理),通过编程的方式与服务端做二进制XML通信" Click="btnHelloCoding_Click" />
</StackPanel>
</Grid>
</navigation:Page>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="BinaryXml Page">
<Grid x:Name="LayoutRoot">
<StackPanel Orientation="Horizontal" Height="30">
<!--支持二进制 XML 通信-->
<TextBox x:Name="txtName" Text="webabcd" />
<Button x:Name="btnHelloConfig" Content="引用服务后(使用代理),通过配置的方式与服务端做二进制XML通信" Click="btnHelloConfig_Click" />
<Button x:Name="btnHelloCoding" Content="引用服务后(使用代理),通过编程的方式与服务端做二进制XML通信" Click="btnHelloCoding_Click" />
</StackPanel>
</Grid>
</navigation:Page>
BinaryXml.xaml.cs
ServiceReferences.ClientConfig
<
configuration
>
< system.serviceModel >
< bindings >
< customBinding >
< binding name ="CustomBinding_BinaryXmlService" >
< binaryMessageEncoding />
< httpTransport maxReceivedMessageSize ="2147483647" maxBufferSize ="2147483647" />
</ binding >
</ customBinding >
</ bindings >
< client >
< endpoint address ="http://localhost:8616/BinaryXmlService.svc"
binding ="customBinding" bindingConfiguration ="CustomBinding_BinaryXmlService"
contract ="BinaryXmlService.BinaryXmlService" name ="CustomBinding_BinaryXmlService" />
</ client >
</ system.serviceModel >
</ configuration >
< system.serviceModel >
< bindings >
< customBinding >
< binding name ="CustomBinding_BinaryXmlService" >
< binaryMessageEncoding />
< httpTransport maxReceivedMessageSize ="2147483647" maxBufferSize ="2147483647" />
</ binding >
</ customBinding >
</ bindings >
< client >
< endpoint address ="http://localhost:8616/BinaryXmlService.svc"
binding ="customBinding" bindingConfiguration ="CustomBinding_BinaryXmlService"
contract ="BinaryXmlService.BinaryXmlService" name ="CustomBinding_BinaryXmlService" />
</ client >
</ system.serviceModel >
</ configuration >
2、本地连接的演示
Silverlight 程序 1
LocalConnection.xaml
Silverlight 程序 1
LocalConnection.xaml
<navigation:Page x:Class="Silverlight30.Communication.LocalConnection"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="LocalConnection Page">
<Grid x:Name="LayoutRoot">
<StackPanel>
<!--结合 Silverlight30.LocalConnection/MainPage.xaml 中的项目演示 Silverlight 对本地连接的支持-->
<TextBlock Text="我是 abc" />
<Button x:Name="btnSubmit" Content="提交" Click="btnSubmit_Click" />
<TextBlock x:Name="lblResult" />
</StackPanel>
</Grid>
</navigation:Page>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="LocalConnection Page">
<Grid x:Name="LayoutRoot">
<StackPanel>
<!--结合 Silverlight30.LocalConnection/MainPage.xaml 中的项目演示 Silverlight 对本地连接的支持-->
<TextBlock Text="我是 abc" />
<Button x:Name="btnSubmit" Content="提交" Click="btnSubmit_Click" />
<TextBlock x:Name="lblResult" />
</StackPanel>
</Grid>
</navigation:Page>
LocalConnection.xaml.cs
Silverlight 程序 2
Silverlight30.LocalConnection/MainPage.xaml
Silverlight30.LocalConnection/MainPage.xaml
<UserControl x:Class="Silverlight30.LocalConnection.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<StackPanel>
<!--结合 Silverlight30/Communication/LocalConnection.xaml 中的项目演示 Silverlight 对本地连接的支持-->
<TextBlock Text="我是 xyz" />
<Button x:Name="btnSubmit" Content="提交" Click="btnSubmit_Click" />
<TextBlock x:Name="lblResult" />
</StackPanel>
</Grid>
</UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<StackPanel>
<!--结合 Silverlight30/Communication/LocalConnection.xaml 中的项目演示 Silverlight 对本地连接的支持-->
<TextBlock Text="我是 xyz" />
<Button x:Name="btnSubmit" Content="提交" Click="btnSubmit_Click" />
<TextBlock x:Name="lblResult" />
</StackPanel>
</Grid>
</UserControl>
Silverlight30.LocalConnection/MainPage.xaml.cs
以上两个 Silverlight 程序间可以进行本地通信
Silverlight30.LocalConnectionTestPage.html
Silverlight30.LocalConnectionTestPage.html
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
height="100%" style="float: left; width: 50%">
< param name ="source" value ="ClientBin/Silverlight30.xap" />
< param name ="onError" value ="onSilverlightError" />
< param name ="background" value ="white" />
< param name ="minRuntimeVersion" value ="3.0.40624.0" />
< param name ="autoUpgrade" value ="true" />
< a href ="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style ="text-decoration: none" >
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
style="border-style: none" />
</a>
</object>
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
height="100%" style="float: left; width: 50%">
< param name ="source" value ="ClientBin/Silverlight30.LocalConnection.xap" />
< param name ="onError" value ="onSilverlightError" />
< param name ="background" value ="white" />
< param name ="minRuntimeVersion" value ="3.0.40624.0" />
< param name ="autoUpgrade" value ="true" />
< a href ="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style ="text-decoration: none" >
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
style="border-style: none" />
</a>
</object>
height="100%" style="float: left; width: 50%">
< param name ="source" value ="ClientBin/Silverlight30.xap" />
< param name ="onError" value ="onSilverlightError" />
< param name ="background" value ="white" />
< param name ="minRuntimeVersion" value ="3.0.40624.0" />
< param name ="autoUpgrade" value ="true" />
< a href ="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style ="text-decoration: none" >
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
style="border-style: none" />
</a>
</object>
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
height="100%" style="float: left; width: 50%">
< param name ="source" value ="ClientBin/Silverlight30.LocalConnection.xap" />
< param name ="onError" value ="onSilverlightError" />
< param name ="background" value ="white" />
< param name ="minRuntimeVersion" value ="3.0.40624.0" />
< param name ="autoUpgrade" value ="true" />
< a href ="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style ="text-decoration: none" >
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
style="border-style: none" />
</a>
</object>
OK
[源码下载]
[源码下载]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。