Announcement

All important changes of my site and my blog will be announced here.

Computer security

This is my hobby. I spend my freetime for it. I am not professional in this area. But I like it very much.

Everything Else

Which doesn’t belong to other categories will land here. It maybe about my daily life, entertainment, music, game … or something like that

Programming

This is my job. That means everyday I must sit before the monitor. Tip something, be annoyed by bugs,… but I simply like it.

Tutorial

When I found something interesting, I would like to share it to everyone. That is the reason for this category comes.

Home » Programming

Silverlight – Sending data between Silverlight applications with LocalMessage API

Submitted by on Monday, 8 March 2010No Comment | 1,191 views

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

Popularity: 2% [?]

My strongly recommended books to read. Choose one and enjoy yourself.

Related Posts:

  • No Related Posts

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.