Multiple Silverlight applications running on the same computer can communicate with each other through local messages. Communication is performed on the client and does not need to interact with the server. This feature enables communication between multiple Silverlight applications hosted on the same page or between Silverlight applications hosted on different pages, the Silverlight application hosted on the web page can also communicate with other applications running outside the browser.
To communicate between Silverlight applications, you must create an information sender and an information receiver. The sender uses LocalMessageSender to send information, and the receiver uses LocalMessageReceiver to receive information.
Information Sender:
When creating a sender, you must provide a receiver name. You can also specify the receiver's domain. If the receiver's domain is not provided, the Global domain is used by default.
View sourceprint? 01 public class MainViewModel: ViewModelBase
02 {
03 private LocalMessageSender messageSender;
04 private const int MAXRETRY = 10;
05 private int retry = 1;
06
07 public string SendResult
08 {
09 get;
10 private set;
11}
12
13 /// <summary>
14 // Initializes a new instance of the MainViewModel class.
15 /// </summary>
16 public MainViewModel ()
17 {
18 SendMessageCommand = new RelayCommand <string> (SendMessage );
19 messageSender = new LocalMessageSender ("receiver1 ");
20 messageSender. SendCompleted + = new System. EventHandler <SendCompletedEventArgs> (messageSender_SendCompleted );
21}
22
23 private void messageSender_SendCompleted (object sender, SendCompletedEventArgs e)
24 {
25 if (e. Error! = Null)
26 {
27 if (retry> MAXRETRY)
28 {
29 SendResult = "cocould not send message .";
30 RaisePropertyChanged ("SendResult ");
31 return;
32}
33 else
34 {
35 retry ++;
36 SendMessage (e. Message );
37}
38}
39 else
40 {
41 retry = 1;
42 SendResult = string. Concat (e. ReceiverName, ":", e. Response );
43 RaisePropertyChanged ("SendResult ");
44}
45}
46
47 public RelayCommand <string> SendMessageCommand {get; private set ;}
48 private void SendMessage (string msg)
49 {
50 messageSender. SendAsync (msg );
51}
52}
The SendAsync method is used to send information. When the message is successfully sent, the SendCompleted event is triggered. You can use the Response attribute of SendCompletedEventArgs to obtain the information returned from the receiving end. If the message fails to be sent, SendFailedException is thrown, you can obtain this information through the Error attribute of SendCompletedEventArgs.
Information receiver:
When creating an acceptor, you must specify the acceptor name. You can also specify whether the range of the acceptor name is global, whether it is a specific domain of the receiver, and whether the receiver can receive messages from the specified domain. The acceptor name must be globally unique or unique in the host domain of the acceptor.
View sourceprint? 01 public class MainViewModel: ViewModelBase
02 {
03 private LocalMessageReceiver messageReceiver;
04
05 public string Message
06 {
07 get;
08 private set;
09}
10
11 /// <summary>
12 // Initializes a new instance of the MainViewModel class.
13 /// </summary>
14 public MainViewModel ()
15 {
16 messageReceiver = new LocalMessageReceiver ("receiver1 ");
17 messageReceiver. MessageReceived + = new System. EventHandler <MessageReceivedEventArgs> (messageReceiver_MessageReceived );
18 try
19 {
20 messageReceiver. Listen ();
21}
22 catch (ListenFailedException e)
23 {
24 Message = e. Message;
25 RaisePropertyChanged ("Message ");
26}
27}
28
29 private void messageReceiver_MessageReceived (object sender, MessageReceivedEventArgs e)
30 {
31 e. Response = "Message received ed .";
32 Message = e. Message;
33 RaisePropertyChanged ("Message ");
34}
35}
The receiving end listens for information through the Listen method. When the Message is received, the MessageReceived event is triggered. You can obtain the received information through the Message attribute of MessageReceivedEventArgs, you can also set the Response attribute of MessageReceivedEventArgs to return a message to the sender.