Silverlight 程序之:基于 Silverlight 的本地应用程序之间实现通信
概述
LocalMessageSender 类
表示两个基于 Silverlight 的应用程序之间的本地消息传递通道的发送端。
一个 LocalMessageSender 对象可以向在同一台计算机上运行的其他基于 Silverlight 的应用程序中的 LocalMessageReceiver 发送消息。
LocalMessageSender.SendAsync 方法 (String)
通过异步方式向配置的接收方发送指定的消息。
LocalMessageSender.SendCompleted 事件
当消息成功发送时发生。
可以处理此事件以确定消息是否已成功发送并检索任何响应消息。如果未收到消息,则 AsyncCompletedEventArgs.Error 属性将设置为 SendFailedException 实例。例如,如果指定的接收器名称尚未注册,或者接收器未配置为从发送器的域接收消息,就可能会发生上述情况。
LocalMessageReceiver.Listen 方法
开始监听来自 LocalMessageSender 的消息。
此方法注册 LocalMessageReceiver 并使其能够接收消息。
调用此方法后,当已配置为向此接收方发送消息的 LocalMessageSender 调用其 SendAsync 方法时,会发生 MessageReceived 事件。
要停止接收消息,必须调用 Dispose 方法。
效果图
建立如图解决方案
发送内容xaml代码:
<Grid x:Name="LayoutRoot">
<Border BorderThickness="2"
BorderBrush="#FF101010"
CornerRadius="4"
Margin="30">
<StackPanel Margin="30">
<TextBlock Text="发送内容:"
FontSize="20" />
<TextBox x:Name="MessageText"
FontSize="20" />
<TextBlock x:Name="txtLength"></TextBlock>
</StackPanel>
</Border>
</Grid>
发送内容cs代码:
namespace LocalConnectionExample
{
public partial class MainPage : UserControl
{
private LocalMessageSender _sender = new LocalMessageSender("InAction");
public MainPage()
{
InitializeComponent();
MessageText.TextChanged += new TextChangedEventHandler(MessageText_TextChanged);
}
void MessageText_TextChanged(object sender, TextChangedEventArgs e)
{
_sender.SendAsync(MessageText.Text);
txtLength.Text = "发送的内容为" + MessageText.Text.Length + "个字符";
}
}
}
接受内容xaml代码:
<Grid x:Name="LayoutRoot">
<Border BorderThickness="2"
BorderBrush="#FF101010"
CornerRadius="4"
Margin="30">
<StackPanel Margin="30">
<TextBlock Text="接受内容:"
FontSize="20" />
<TextBox x:Name="MessageText"
FontSize="20" />
<TextBlock x:Name="txtLength"></TextBlock>
</StackPanel>
</Border>
</Grid>
接受内容CS代码:
namespace LocalConnectionReceiver
{
public partial class MainPage : UserControl
{
private LocalMessageReceiver _receiver = new LocalMessageReceiver("InAction");
public MainPage()
{
InitializeComponent();
_receiver.MessageReceived += new EventHandler<MessageReceivedEventArgs>(_receiver_MessageReceived);
_receiver.Listen();
}
void _receiver_MessageReceived(object sender, MessageReceivedEventArgs e)
{
MessageText.Text = e.Message;
txtLength.Text = "接受的内容为" + e.Message.Length + "个字符";
if(e.Message.Length>=10)
_receiver.Dispose();
}
}
}
web显示页代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>LocalConnectionExample</title>
<style type="text/css">
html, body {
height: 100%;
overflow: auto;
}
body {
padding: 0;
margin: 0;
}
#silverlightControlHost {
height: 100%;
text-align:center;
}
</style>
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript">
function onSilverlightError(sender, args) {
var appSource = "";
if (sender != null && sender != 0) {
appSource = sender.getHost().Source;
}
var errorType = args.ErrorType;
var iErrorCode = args.ErrorCode;
if (errorType == "ImageError" || errorType == "MediaError") {
return;
}
var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n" ;
errMsg += "Code: "+ iErrorCode + " \n";
errMsg += "Category: " + errorType + " \n";
errMsg += "Message: " + args.ErrorMessage + " \n";
if (errorType == "ParserError") {
errMsg += "File: " + args.xamlFile + " \n";
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
else if (errorType == "RuntimeError") {
if (args.lineNumber != 0) {
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
errMsg += "MethodName: " + args.methodName + " \n";
}
throw new Error(errMsg);
}
</script>
</head>
<body>
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost" style="height:300px">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/LocalConnectionExample.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50401.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
<div id="silverlightControlHost2" style="height:300px;width:100%;position:absolute">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/LocalConnectionReceiver.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50401.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="Iframe1" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
</form>
</body>
</html>
大功告成!!!!
作者:记忆逝去的青春
出处:http://www.cnblogs.com/lukun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过http://www.cnblogs.com/lukun/ 联系我,非常感谢。