I’d like to provide you with a SilverBullet™, a small snippet of Silverlight, a class or namespace hidden in the silverlight .NET framework, to help you out in times of need. It’s not to learn, but something to keep in your pocket. Just remember it’s there and you’re safe.
Most often Silverlight applications take up the whole screen and the page that is hosting the application contains only one control. It is possible to split you application into smaller controls. This can be valuable when you are using silverlight 3 in SharePoint parts, or just to leverage your webpage with a few nice looking controls. It’s even possible to take this a whole mile further. Joe Stegman gave an awesome demonstration of this at the Mix09 event earlier this year. Silverlight 3 controls can communicate over the boundaries of tabs inside a browser, and even over the boundaries of the browsers itself. Sending messages back and forth between IE, Firefox, Chrome and even Out-Of-Browser. To get this all to work you have to take a look at the System.Windows.Messaging namespace.
The namespace contains only a few classes of which these two are the most important:
It is not hard to guess what these do, the first one sends messages and the second one receives them. Both classes are not hard to understand. Let me explain with a bit of code.
public void someMethod()
LocalMessageSender myMessageSender =
myMessageSender.SendCompleted += myMessageSenderSendCompleted;
myMessageSender.SendAsync("a message from control 1");
void myMessageSenderSendCompleted(object sender, SendCompletedEventArgs e)
Console.WriteLine("Send to " + e.ReceiverName + "->" + e.Message);
When the someMethod() method is called, a new LocalMessageSender object is created. The parameter “myReceiverName” is the identifier for the communication between the Silverlight controls.
After sending a message, the SendComplete event is fired. In this example it is handled my the myMessageSenderSendCompleted event handler and a string containing the message and the receiver name are written to the console.
Then SendASync method sends a message.The message can only be a string, but may take up to 1 Mb in size.
Receiving messages is as easy as sending them:
void MainPage_Loaded(object sender, RoutedEventArgs e)
myMessageReceiver = new LocalMessageReceiver("myReceiverName");
myMessageReceiver.MessageReceived += myMessageReceiverMessageReceived;
void myMessageReceiverMessageReceived(object sender,
Console.WriteLine("Received from " + e.ReceiverName + "->" + e.Message);
In the Loaded event handler of a UserControl a new instance of a LocalMessageReceiver is created. It is given the same string as a parameter as the Sender in the earlier example, “myReceiverName”.
When a message is received, the MessageReceived event is raised, in this example it is handled my the myMessageReceiverMessageReceived event handler, which writes the message it received and the name of the receiver to the console.
The last method, Listen(), starts the listener. From now on messages can be received.
Tags van Technorati: Silverlight