Silverlight – Sending data between Silverlight applications with LocalMessage API

In Silverlight 3, Microsoft allows us to sending data between local Silverlight-based applications through defined channel. To use this feature we’ll apply the classes in namespace System.Windows.Messaging. In next example I would like to illustrate how we can build this local channel.

1. Start Microsoft Visual Studio and create a Silverlight application. Let’s VS create a web project to test our Silverlight application too. I name the Silverlight application as “Data Sender” and the web project as “Sending data between SVL Applications.Web”.
2. Adding to current solution, a new Silverlight application project called “Data Receiver”. Use the available web project to host this new Silverlight application too.
3. Open Microsoft Expression Blend 3 and edit main page of Data Sender as below

4. Rename the control as following

5. Do 2 steps above for Data Receiver

6. Add handler for Click-event of btnSend in Data Sender and add following code

LocalMessageSender m_lmsSender = new LocalMessageSender("Communication");
private void btnSend_Click(object sender, System.Windows.RoutedEventArgs e)
{
	// TODO: Add event handler implementation here.
	m_lmsSender.SendAsync(txtMessage.Text);
}

LocalMesasgeSender class represents the sending end of a local messaging channel between two Silverlight-based applications.In our example, we built a local channel “Communication” for talking with the other apps.

7. On the code side of Data Receiver, add handler for UserControl-Loaded and insert code

LocalMessageReceiver m_lmrReceiver = new LocalMessageReceiver("Communication");
private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
	// TODO: Add event handler implementation here.
	m_lmrReceiver.MessageReceived += new EventHandler<MessageReceivedEventArgs>(m_lmrReceiver_MessageReceived);
	m_lmrReceiver.Listen();
}

LocalMessageReceiver class represents the receiving end of a local messaging channel between two Silverlight-based applications. Adding handler to MessageReceived-Event allows us to catch and handle data in the way we want. After finishing with initialization, start to listen for incoming message and be sure that the name of channel must be same as in Data Sender, otherwise you can not receive anything.

8. Show the received message on Receiver

void m_lmrReceiver_MessageReceived(object sender, MessageReceivedEventArgs e)
{
	txtMessage.Text = e.Message;
}

9. Run and test our example by adding 2 application into one test page. In our example, first set the start page for web project. Right click on web project –> Properties –> Web –> Start Action.

Then edit its content as following

<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="50%">
  <param name="source" value="ClientBin/Data Sender.xap"/>
  <param name="onError" value="onSilverlightError" />
  <param name="background" value="white" />
  <param name="minRuntimeVersion" value="3.0.40818.0" />
  <param name="autoUpgrade" value="true" />
  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.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" width="100%" height="100%">
  <param name="source" value="ClientBin/Data Receiver.xap"/>
  <param name="onError" value="onSilverlightError" />
  <param name="background" value="white" />
  <param name="minRuntimeVersion" value="3.0.40818.0" />
  <param name="autoUpgrade" value="true" />
  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
	  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" 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>

The complete source code you can download in following link “Sending data between SVL applications

Leave a Reply

Your email address will not be published. Required fields are marked *