IntroductionOne of the features that I’m surprised to see hardly no one talk about is “Communication Between Local Silverlight-Based Applications”. This is not new to Silverlight as it has been around since Silverlight 3. In other words, this will allow you to have two Silverlight applications running on the same PC talk to one another without using Web Services, etc. I originally was looking into this for a pet project that I was going to use with Kinect, but found this very valuable and decided to share with everyone. Getting StartedWe are going to create two separate Silverlight Applications (select Silverlight 4 or 5 Beta). The First application that we are going to create is the receiver.Creating the Receiver Launch Visual Studio 2010 and create a new Silverlight Application and name it “SilverlightReceiver”.In your MainPage.xaml <Grid x:Name="LayoutRoot" Background="Yellow">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Text="...Waiting for Message!" x:Name="txtReceiver"/>
</Grid>
In your MainPage.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Messaging;
namespace SilverlightReceiver
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var messageReceiver = new LocalMessageReceiver("receiver", ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
messageReceiver.MessageReceived += receiver_MessageReceived;
try
{
messageReceiver.Listen();
}
catch (ListenFailedException)
{
MessageBox.Show(
"Cannot receive messages." + Environment.NewLine +
"There is already a receiver with the name 'receiver'.",
"LocalMessageReceiver", MessageBoxButton.OK);
}
}
private void receiver_MessageReceived(object sender, MessageReceivedEventArgs e)
Read more: Michael Crump
QR:
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Text="...Waiting for Message!" x:Name="txtReceiver"/>
</Grid>
In your MainPage.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Messaging;
namespace SilverlightReceiver
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var messageReceiver = new LocalMessageReceiver("receiver", ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
messageReceiver.MessageReceived += receiver_MessageReceived;
try
{
messageReceiver.Listen();
}
catch (ListenFailedException)
{
MessageBox.Show(
"Cannot receive messages." + Environment.NewLine +
"There is already a receiver with the name 'receiver'.",
"LocalMessageReceiver", MessageBoxButton.OK);
}
}
private void receiver_MessageReceived(object sender, MessageReceivedEventArgs e)
Read more: Michael Crump
QR:
0 comments:
Post a Comment