15.10. Local Connections

Some page designs utilize two separate Silverlight controls and need a way to pass information between them. Previously, this could be accomplished only using the HTML DOM methods. Silverlight 3.0 makes this much easier with the local connection API. Let's create a simple example and send some text from one Silverlight control to another:

  1. Open Visual Studio, and create a new Silverlight application called Chapter15.Sender.

  2. Add another Silverlight application to the solution called Chapter15.Receiver (opt not to create another web hosting project).

  3. Open Chapter15.SenderTestPage.aspx.

  4. You want to display the Chapter15.Receiver project on the same page, so add another Silverlight control beneath the existing one by copying the object block and modifying the source parameter to display the Chapter15.Receiver project.xap file:

    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param name="source" value="ClientBin/Chapter15.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=161376" alt="Get Microsoft Silverlight"
    style="border-style:none"/>
      </a>
    </object>

  5. Open MainPage.xaml in the sender project, and add a button with the following XAML:

    <Button x:Name="cmdSendMessage" Content="Send Message" Width="200"
    Height="200"></Button>

  6. Open MainPage.xaml.cs in the sender project, and import the following namespace:

    using System.Windows.Messaging;

  7. Amend the code in MainMenu.xaml.cs to the following:

    public partial class MainPage : UserControl
    {
        LocalMessageSender Channel1 = new LocalMessageSender("Channel1");
    
        public MainPage()
        {
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            Channel1.SendCompleted +=
              new EventHandler<SendCompletedEventArgs>(Channel1_SendCompleted);
            InitializeComponent();
        }
    
        void Channel1_SendCompleted(object sender, SendCompletedEventArgs e)
        {
            //Code to run after message is sent
        }
    
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.cmdSendMessage.Click += new RoutedEventHandler(cmdSendMessage_Click);
        }
    
        void cmdSendMessage_Click(object sender, RoutedEventArgs e)
        {
            Channel1.SendAsync("Hello from sender project");
        }
    }

  8. Open the receiver project MainPage.xaml, and add a text box to display the received messages:

    <TextBlock>Received messages:</TextBlock>
    <TextBox x:Name="txtMessage"></TextBox>

  9. You now need to add code to receive the messages and update the text box with the messages you have received. To receive messages, handle the MessageReceived event on an instance of LocalMessageReceiver. Open MainPage.xaml.cs in the receiver project, and import the following namespace:

    using System.Windows.Messaging;

  10. Amend MainPage.xaml.cs to the following:

    public partial class MainPage : UserControl
    {
        LocalMessageReceiver Channel1Receiver = new LocalMessageReceiver("Channel1");
    
        public MainPage()
        {
    
            Channel1Receiver.MessageReceived +=
              new EventHandler<MessageReceivedEventArgs>(
                Channel1Receiver_MessageReceived
              );
            Channel1Receiver.Listen();
            InitializeComponent();
        }
    
        void Channel1Receiver_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            txtMessage.Text="" + e.Message.ToString();
        }
    }

    NOTE

    The LocalMessageReceiver constructor sets the parameter to Channel1 (the same channel that messages are being sent on).

  11. Press F5 to run the project.

  12. Click the Send Message button. A message should then be sent using the local connection API, and the results should be displayed in the text box in the receiver project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.137.75