如何在基于 Silverlight 的本地应用程序之间实现通信
2010-02-05 14:24 Virus-BeautyCode 阅读(333) 评论(0) 编辑 收藏 举报如何在基于 Silverlight 的本地应用程序之间实现通信
下面的代码示例演示如何使用 LocalMessageSender 和 LocalMessageReceiver 类。
在 XAML 中,Receiver 类定义 TextBlock 以便显示它接收的消息。Sender 类定义 Button 以便发送消息,并且定义 TextBlock 以便显示它接收的响应。
在代码隐藏中,Receiver 类初始化 LocalMessageReceiver 并处理 MessageReceived 事件。Sender 类初始化 LocalMessageSender 并为发送消息提供自定义处理。这使得发送器可以反复发送消息,直到接收器响应。这在某些情况下很有用,例如在接收应用程序的加载时间较长时。
发送器将尝试次数随消息一起传递到 SendAsync(String, Object) 方法调用。在接收器最终响应时,发送器将该尝试次数与响应一起显示。
该 HTML 示例承载发送应用程序的两个副本和接收应用程序的一个副本。这说明接收器可以接收来自多个发送器的消息。
代码using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Messaging;
namespace SendingApplication
{
public partial class Sender : UserControl
{
private LocalMessageSender messageSender;
public Sender()
{
InitializeComponent();
UpdateButton();
messageSender = new LocalMessageSender(
"receiver", LocalMessageSender.Global);
messageSender.SendCompleted += sender_SendCompleted;
SendMessage("message from Sender constructor");
}
private int clickNumber = 1;
private void UpdateButton()
{
button.Content = "send message 'click " + clickNumber + "'";
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SendMessage("click " + clickNumber);
clickNumber++;
UpdateButton();
}
private const int MAX_ATTEMPTS = 10000;
private int attempt = 1;
private void SendMessage(string message)
{
messageSender.SendAsync(message, attempt);
}
private void sender_SendCompleted(object sender, SendCompletedEventArgs e)
{
if (e.Error != null)
{
LogError(e);
attempt++;
if (attempt > MAX_ATTEMPTS)
{
output.Text = "Could not send message.";
return;
}
SendMessage(e.Message);
return;
}
output.Text =
"Message: " + e.Message + Environment.NewLine +
"Attempt " + (int)e.UserState +
" completed." + Environment.NewLine +
"Response: " + e.Response + Environment.NewLine +
"ReceiverName: " + e.ReceiverName + Environment.NewLine +
"ReceiverDomain: " + e.ReceiverDomain;
// Reset attempt counter.
attempt = 1;
}
private void LogError(SendCompletedEventArgs e)
{
System.Diagnostics.Debug.WriteLine(
"Attempt number {0}: {1}: {2}", (int)e.UserState,
e.Error.GetType().ToString(), e.Error.Message);
}
}
}
代码
using System;
using System.Windows.Controls;
using System.Windows.Messaging;
namespace ReceivingApplication
{
public partial class Receiver : UserControl
{
public Receiver()
{
InitializeComponent();
LocalMessageReceiver messageReceiver =
new LocalMessageReceiver("receiver",
ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
messageReceiver.MessageReceived += messageReceiver_MessageReceived;
try
{
messageReceiver.Listen();
}
catch (ListenFailedException)
{
output.Text = "Cannot receive messages." + Environment.NewLine +
"There is already a receiver with the name 'receiver'.";
}
}
private void messageReceiver_MessageReceived(
object sender, MessageReceivedEventArgs e)
{
e.Response = "response to " + e.Message;
output.Text =
"Message: " + e.Message + Environment.NewLine +
"NameScope: " + e.NameScope + Environment.NewLine +
"ReceiverName: " + e.ReceiverName + Environment.NewLine +
"SenderDomain: " + e.SenderDomain + Environment.NewLine +
"Response: " + e.Response;
}
}
}
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<!-- saved from url=(0014)about:internet -->
<head>
<title>LocalMessaging</title>
<style type="text/css">
html, body {
height: 100%;
overflow: auto;
}
body {
padding: 0;
margin: 0;
}
#silverlightControlHost1 {
padding: 0;
margin: 0;
}
#silverlightControlHost2 {
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<table border="10" cellpadding="10" cellspacing="10">
<tr>
<td>
<div id="silverlightControlHost1">
<object data="data:application/x-silverlight-2,"
type="application/x-silverlight-2"
width="400" height="120">
<param name="source" value="ClientBin/SendingApplication.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
</object>
<iframe style='visibility:hidden;height:0;width:0;border:0px'>
</iframe>
</div>
</td>
</tr>
<tr>
<td>
<div id="Div1">
<object data="data:application/x-silverlight-2,"
type="application/x-silverlight-2"
width="400" height="120">
<param name="source" value="ClientBin/SendingApplication.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
</object>
<iframe style='visibility:hidden;height:0;width:0;border:0px'>
</iframe>
</div>
</td>
</tr>
<tr>
<td>
<div id="silverlightControlHost2">
<object data="data:application/x-silverlight-2,"
type="application/x-silverlight-2"
width="400" height="120">
<param name="source" value="ClientBin/ReceivingApplication.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
</object>
<iframe style='visibility:hidden;height:0;width:0;border:0px'>
</iframe>
</div>
</td>
</tr>
</table>
</body>
</html>