Click here to Skip to main content
Click here to Skip to main content

How to Communicate Between Two Local Silverlight Applications?

By , 26 Jul 2011
 
Prize winner in Competition "Best C# article of July 2011"

Introduction

Sometimes, it is required to communicate between two local Silverlight applications. Though this situation is very limited, it may be required as per your business deal. In such a case, the local messaging mechanism comes into the place.

In this post, we will learn about this local messaging system using a small Silverlight application where we will have a sender project and a receiver project. At the end of the article, I embedded the demo which will give you more visibility on what we are going to do in the demo. Let's discuss more about this topic.

Create Project Structure

Let's start by creating the project structure for our sample demo app. In this demo, we will create two different Silverlight projects called "Sender" and "Receiver". Sender project will broadcast some message and the receiver will catch that message and do the next operation according to that.

Here is the project structure for your reference:

Silverlight Local Messaging - Project Structure

Create the Sender UI

Let us create the UI of the sender application. Here we will have couple of radio buttons for selecting different colors and a button which enables the user to submit the message to broadcast. Here is the XAML code:

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel>
        <RadioButton x:Name="rdoRed" GroupName="color" Content="Red"/>
        <RadioButton x:Name="rdoGreen" GroupName="color" Content="Green"/>
        <RadioButton x:Name="rdoBlue" GroupName="color" Content="Blue"/>
        <RadioButton x:Name="rdoYellow" GroupName="color" Content="Yellow"/>
        <Button Content="Send" Height="26" Width="150" Click="SendButtonClick"/>
    </StackPanel>
</Grid>

This will render as below in the UI:

Silverlight Local Messaging - Sender's UI

Implement Message Broadcasting

It's time to send the message to the receiver application. To do this, there is a class called LocalMessageSender and it is present under "System.Windows.Messaging" namespace, which takes receiver name and receiver domain as constructor parameter:

public LocalMessageSender(string receiverName, string receiverDomain);

On button click, we will check which color has been selected and then we will create the instance of the class LocalMessageSender and pass proper color as message to the SendAsync() method as implemented below:

private void SendButtonClick(object sender, RoutedEventArgs e)
{
    var rdoRedSelected = rdoRed.IsChecked == true;
    var rdoGreenSelected = rdoGreen.IsChecked == true;
    var rdoBlueSelected = rdoBlue.IsChecked == true;
    var rdoYellowSelected = rdoYellow.IsChecked == true;
 
    var localMessageSender = new LocalMessageSender("myMessagingDemo", 
                                                     LocalMessageSender.Global);
    localMessageSender.SendAsync(rdoRedSelected ? "Red" 
                                                : rdoGreenSelected ? "Green" 
                                                : rdoBlueSelected ? "Blue" 
                                                : rdoYellowSelected ? "Yellow"
                                                : "White");
}

It has completed event (SendCompleted) for the SendAsync() method where you can check ReceiverName, ReceiverDomain, Message and Response if any.

Implementation of Message Receiving

Similar to the LocalMessageSender, we have another class called LocalMessageReceiver present in the same namespace called "System.Windows.Messaging". It takes receiver name, receiver's namescope and allowed sender domains as constructor parameters as shown below:

public LocalMessageReceiver(string receiverName, 
                                   ReceiverNameScope nameScope, 
                                   IEnumerable<string> allowedSenderDomains);

In the page loaded event of receiver project, we will create the instance of the LocalMessageReceiver, register the MessageReceived event and then call the Listen() method to start listening to the message.

void MainPageLoaded(object sender, RoutedEventArgs e)
{
    var localMessageReceiver = new LocalMessageReceiver("myMessagingDemo", 
                                                         ReceiverNameScope.Global, 
                                                         LocalMessageReceiver.AnyDomain);
    localMessageReceiver.MessageReceived += LocalMessageReceiverMessageReceived;
    localMessageReceiver.Listen();
}

In the message received event implementation, we will check the type of message and based on the received message, we will do proper operation. In our case, based on the color information that we received, we will change the color of the background of the LayoutRoot grid of the receiver application.

Here is the implementation of the same:

void LocalMessageReceiverMessageReceived(object sender, MessageReceivedEventArgs e)
{
    var message = e.Message;
 
    switch (message)
    {
        case "Red":
            LayoutRoot.Background = new SolidColorBrush(Colors.Red);
            break;
        case "Green":
            LayoutRoot.Background = new SolidColorBrush(Colors.Green);
            break;
        case "Blue":
            LayoutRoot.Background = new SolidColorBrush(Colors.Blue);
            break;
        case "Yellow":
            LayoutRoot.Background = new SolidColorBrush(Colors.Yellow);
            break;
        default:
            LayoutRoot.Background = new SolidColorBrush(Colors.White);
            break;
    }
}

That's all about the implementation part. Let's see it in action.

Demo

Let's run our application. As we have two projects, run each of them one by one. Once run, you will have two browser windows having one as Sender application and the other as Receiver application. By default, the receiver will have white background. Now go to the sender application and choose any one of the radio buttons to select the color. Then click "Send" button to send the color message to the other Silverlight application. The receiver application will catch the message based on the name and change the color of the layout accordingly.

Have a look into the demo of the same here which will give you proper visibility to the implementation:

Silverlight Local Messaging Demo

Hope this helped you to understand the local messaging system of Silverlight application. Remember that this will not work when your browser is in Private mode.

History

  • 26th July, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

_ Kunal Chowdhury _
Software Developer
India India
Kunal Chowdhury is a Microsoft MVP (Most Valuable Professional) in Silverlight Technology, a Codeproject MVP & Mentor, DZone MVB (Most Valuable Blogger), Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working as a Software Engineer II in an MNC located at Pune, India. He has a very good skill over XAML, C#, Silverlight and WPF. He has a good working experience in Windows 7 application (including Multi-touch) development too.
 
He posts his findings in his technical blog. He also writes for SilverlightShow and Codeproject portal. Many of his articles were highlighted as "Article of the Day" in Microsoft sites.
 
He also has another website called Silverlight-Zone.com where he posts article links on Silverlight, Windows Phone 7 and XNA accumulated from various web sites to help the community grow on specified technologies.
 
You can reach him in his Blog : http://www.kunal-chowdhury.com
He is also available in Twitter : http://twitter.com/kunal2383
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5 PinmemberCarsten V2.04-Feb-13 5:45 
GeneralMy vote of 5 PinmemberChristian Amado28-Jul-12 13:09 
GeneralMy vote of 5 PinmemberUday P.Singh5-Dec-11 20:59 
GeneralRe: My vote of 5 Pinmvp_ Kunal Chowdhury _5-Dec-11 21:11 
QuestionDiff Domains PinmemberJayesh Modha22-Aug-11 11:28 
AnswerRe: Diff Domains Pinmvp_ Kunal Chowdhury _22-Aug-11 16:32 
GeneralMy vote of 5 PinmemberDurgaprasadtb16-Aug-11 1:47 
GeneralRe: My vote of 5 Pinmvp_ Kunal Chowdhury _16-Aug-11 4:09 
GeneralMy vote of 5 Pinmvpthatraja8-Aug-11 17:00 
GeneralRe: My vote of 5 PinmvpKunal_Chowdhury8-Aug-11 17:48 
GeneralRe: My vote of 5 Pinmvpthatraja8-Aug-11 17:56 
GeneralRe: My vote of 5 PinmvpKunal_Chowdhury8-Aug-11 18:03 
GeneralMy vote of 5 PinmemberKanasz Robert7-Aug-11 21:48 
GeneralRe: My vote of 5 PinmvpKunal_Chowdhury8-Aug-11 1:19 
GeneralMy vote of 5 PinmemberFilip D'haene6-Aug-11 5:40 
GeneralRe: My vote of 5 PinmvpKunal_Chowdhury8-Aug-11 1:19 
GeneralMy vote of 5 Pinmembereshaq6-Aug-11 3:50 
GeneralRe: My vote of 5 PinmvpKunal_Chowdhury8-Aug-11 1:18 
QuestionSuggestion for a future article Pinmemberdbrenth2-Aug-11 3:42 
GeneralMy vote of 5 Pinmembershakti53851-Aug-11 22:41 
GeneralRe: My vote of 5 PinmvpKunal_Chowdhury4-Aug-11 1:44 
GeneralMixed feelings PinmemberMike Klimentiev1-Aug-11 11:42 
GeneralRe: Mixed feelings PinmvpKunal_Chowdhury1-Aug-11 14:36 
GeneralRe: Mixed feelings PinmemberMike Klimentiev2-Aug-11 13:49 
GeneralRe: Mixed feelings PinmemberPaul Conrad5-Aug-11 11:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 26 Jul 2011
Article Copyright 2011 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid