Monday, June 14, 2010

Communicating between Silverlight Applications (Silverlight 3)

You can use the available APIs (LocalMessageSender/LocalMessageReceiver)

Reference: http://elegantcode.com/2010/05/01/communicating-between-silverlight-applications/

The Receiver

This event is fired when a message is successfully received from a sender. The sent message is available in the Message property. The next required step is to call the Listen method on the receiver. The receiver will continue receiving messages until you call its Dispose method.

LocalMessageReceiver messageReceiver = newLocalMessageReceiver(Constants.ReceiverName);
messageReceiver.MessageReceived += (
object sender,MessageReceivedEventArgs e) =>
{
//do something with e.Message
};
messageReceiver.Listen();


After a message is received you can send a response back to the sender by setting the Response property in the MessageReceived event handler.

messageReceiver.MessageReceived += (object sender,MessageReceivedEventArgs e) =>
{
e.Response =
"Message Received: " + e.Message;
};


The Sender

When you create the sender you must specify the receiver that will be listening for the messages. To send a message, the sending application calls the SendAsync method, passing in a String message with a maximum size of 40 kilobytes.

LocalMessageSender sender = newLocalMessageSender(Constants.ReceiverName);
sender.SendAsync(message);

Any response from the receiver can be handled in the SendCompleted event.

sender.SendCompleted += (o, e) =>
{
string response = e.Response;
};

No comments:

Post a Comment