.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2012

How to communicate between Silverlight controls

A while back I discovered a great post about how to establish communication between Silverlight controls on the same page. I had already known how it could be accomplished using Javascript and an example of this approach is used by my Silverlight Gallery. But this other way caught my attention and I have decided to post about it. Jeff Prosise's Blog contains a series of posts called  Cool Silverlight Tricks. Currently there are only 5 tricks, but, they are all great. Make sure you check them out. I will not be discussing the Javascript approach in the blog post. The reader is referred to this post

Jeff basically replaces all the Javascript that would normally be used to establish communication with equivalent Silverlight HTMLBridge calls. Now why didn't I think of that! Let's take a look at some code.

We start by creating a Silverlight project. This project will require two Silverlight controls. I have decide to call the controls SourceSilverlight and TargetSilverlight. The SourceSilverlight control, as it name implies, will be the control that will send commands to the TargetSilverlight. I liked the visual aspect to Jeff's example so I used it here. Both controls will have a circle positioned identically on their respective canvases. When the user moves the circle in the SourceSilverlight control it sends the new postion to the TargetSilverlight control causing the circle to move identically.

Listing 1 contains the code behind for the SourceSilverlight UserControl. I have excluded the XAML markup as it is extremely simple containing an ellipse with a name of ball. The MouseMove, MouseLeftButtonDown and MouseLeftButtonUp events are all wired up and are used to enable dragging.

Listing 1: SourceSilverlight UserControl Code Behind

    1 

    2 Imports System.Windows.Browser

    3 

    4 Partial Public Class Page

    5     Inherits UserControl

    6 

    7     Public Sub New()

    8         InitializeComponent()

    9     End Sub

   10 

   11 #Region "Move Ball Events"

   12     Private isMouseDown As Boolean = False

   13     Private Sub ball_MouseLeftButtonUp( _

   14         ByVal sender As System.Object, _

   15         ByVal e As System.Windows.Input.MouseButtonEventArgs)

   16         isMouseDown = False

   17         CType(sender, Ellipse).ReleaseMouseCapture()

   18     End Sub

   19 

   20     Private Sub ball_MouseLeftButtonDown( _

   21         ByVal sender As System.Object, _

   22         ByVal e As System.Windows.Input.MouseButtonEventArgs)

   23         isMouseDown = True

   24         CType(sender, Ellipse).CaptureMouse()

   25     End Sub

   26 

   27     Private Sub ball_MouseMove( _

   28         ByVal sender As System.Object, _

   29         ByVal e As System.Windows.Input.MouseEventArgs)

   30         If isMouseDown Then

   31             Dim ball As Ellipse = CType(sender, Ellipse)

   32             Dim cx As Double = e.GetPosition(LayoutRoot).X

   33             Dim cy As Double = e.GetPosition(LayoutRoot).Y

   34             ball.SetValue(Canvas.LeftProperty, cx - ((ball.Width) / 2))

   35             ball.SetValue(Canvas.TopProperty, cy - ((ball.Height) / 2))

   36             MoveTargetBall(cx - ((ball.Width) / 2), cy - ((ball.Height) / 2))

   37         End If

   38     End Sub

   39 #End Region

   40 

   41 #Region "Communication to Target Silverlight"

   42     'This is the Jeff Proise's cool silverlight trick. No need for javacript.

   43     'Just call HTML bridge functionality duplicating what you would have done

   44     'in javascript.

   45     Private _target As ScriptObject = Nothing

   46     Public ReadOnly Property Target() As ScriptObject

   47         Get

   48             If _target Is Nothing Then

   49                 'I have hardcoded the id of the target silverlight control.

   50                 'A better solution would be to use the InitParameters feature.

   51                 Dim slhost As HtmlElement = HtmlPage.Document.GetElementById("Xaml2")

   52                 Dim content As ScriptObject = CType(slhost.GetProperty("content"), ScriptObject)

   53                 _target = CType(content.GetProperty("SLScriptObject_TargetSilverlight"), ScriptObject)

   54             End If

   55             Return _target

   56         End Get

   57     End Property

   58 

   59     Private Sub MoveTargetBall(ByVal x As Double, ByVal y As Double)

   60         Target.Invoke("MoveBall", x, y)

   61     End Sub

   62 #End Region

   63 

   64 End Class

I have divided the code into two regions. The first region deals with mouse events and is responsible for moving the circle. Note that at the end of the ball_MouseMove method we call method MoveTargetBall passing as parameters the new position of the circle. I will not discuss how this region of code works as it is a standard drag implementation and is not relevant to the topic at hand. The second region of code deals directly with the communication between the SourceSilverlight and TargetSilverlight control.

Let's examine this code. The real work is done in the ReadOnly Target Property. In this property, we first obtain a reference to the TargetSilverllght control. This is accomplished by calling the GetElementById method passing to it the ID of the TargetSilverlight control. For the purposes of this post I have hard coded this ID, however, a better approach would be to make use the InitParameters. Next using this reference, we call the GetProperty method passing the string content. This returns a reference to a ScriptObject, a representation of a DOM object, and in this case it references the TargetSilverlight control. Finally, using this ScriptObject reference, we make a call to GetProperty this time passing the string  SLScriptObject_TargetSilverlight. What does this string represent you may be asking? Great question, basically, this is the token that was used when the TargetSilverlight control registered a scriptable object.  This will become clearer when we look at the code behind for the TargetSilverlight control.

So all that is required to invoke a registered method from the TargetSilverlight Control is to called the Invoke method passing the string representation of the method and the X and Y position of the circle.

A note of caution. While this approach eliminates any need for Javascript, it comes at a price. This method requires 4 round trips between Silverlight and the browser (lines 51, 52, 53 and 60), whereas, a Javascript approach requires only one round trip.

Listing 2 contains the code behind for the TargetSilverlight control.

List 2: TargetSilverlight Control Code Behind

    1 

    2 Imports System.Windows.Browser

    3 

    4 Partial Public Class Page

    5     Inherits UserControl

    6 

    7     Public Sub New()

    8         InitializeComponent()

    9     End Sub

   10 

   11     <ScriptableMember()> _

   12     Public Sub MoveBall(ByVal x As Double, ByVal y As Double)

   13         ball.SetValue(Canvas.LeftProperty, x)

   14         ball.SetValue(Canvas.TopProperty, y)

   15     End Sub

   16 

   17     Private Sub Page_Loaded( _

   18         ByVal sender As Object, _

   19         ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

   20         HtmlPage.RegisterScriptableObject("SLScriptObject_TargetSilverlight", Me)

   21     End Sub

   22 End Class

Take a look at the MoveBall method, this the method that the SourceSilverlight control called to communicate the new position of the circle (line 60 in listing 1).  Note the use of the ScriptableMemberAttribute. Basically, this makes the method accessible to Javascript callers or in our case via the HTML bridge from another Silverlight control. The Page_Load method calls the RegisterScriptableObject method passing to it the token SLScriptObject_TargetSilverlight and this is the typical pattern for exposing a Silverlight method to a Javascript caller.

The last listing, Listing 3, contains the page markup used to host these two Silverlight controls. Nothing out the ordinary.

Listing 3: Markup used to host the Silverlight controls.

   12 <body style="height:100%;margin:0;">

   13     <form id="form1" runat="server" style="height:100%;">

   14         <asp:ScriptManager ID="ScriptManager1" runat="server">

   15         </asp:ScriptManager>

   16         <div style="float: left; padding-right: 10px; padding-left: 10px; padding-top: 10px;">

   17             <asp:Silverlight

   18               ID="Xaml1"

   19               runat="server"

   20               Source="~/ClientBin/SourceSilverlight.xap"

   21               MinimumVersion="2.0.31005.0" Width="300px" Height="300px" />

   22         </div>

   23         <div style="padding-top: 10px;">

   24             <asp:Silverlight

   25               ID="Xaml2"

   26               runat="server"

   27               Source="~/ClientBin/TargetSilverlight.xap"

   28               MinimumVersion="2.0.31005.0" Width="300px" Height="300px" />

   29         </div>

   30     </form>

   31 </body>

If you would like to see this in action please navigate to this site. 

Guess the movie

Go ahead, Zeus. Throw down a thunderbolt, let the earth swallow me up. I defy you!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Tuesday, November 11, 2008 4:54 PM
Permalink | Comments (61) | Post RSSRSS comment feed

Related posts

Comments

designer bags sale US

Thursday, May 27, 2010 2:34 PM

designer bags sale

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

discount wallets online US

Monday, May 31, 2010 2:27 PM

discount wallets online

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

Cross cutting shredder us

Friday, October 01, 2010 7:38 PM

Cross cutting shredder

Hey =) that was some good reading =)

Shaolin kempo karate was founded by grandmaster frederick j villari

This site is cool. i visit here evaryday.

pantene pro v us

Sunday, October 03, 2010 5:30 PM

pantene pro v

ypuiaqehxloukld, <a href="http://stearnsandfoster.org">stearns and foster</a>, zcoSMxsZigiyZRSilAKU.

Compact refrigerator us

Monday, October 04, 2010 1:18 PM

Compact refrigerator

This site is great. i visit here evaryday.

kathy van zeeland us

Monday, October 04, 2010 2:16 PM

kathy van zeeland

oyxmfscvypnknmn, <a href="http://spaceheaterbuy.com">space heater</a>, MTCQkkApgeErulBvwgwX.

Compact refrigerator us

Wednesday, October 06, 2010 7:19 AM

Compact refrigerator

This site is cool. i visit here evaryday.

Cross cutting shredder us

Thursday, October 07, 2010 1:45 AM

Cross cutting shredder

Please post some more of this =)

Katy Perry (2) HD Wallpaper us

Thursday, October 07, 2010 7:57 PM

Katy Perry  (2) HD Wallpaper

Thanks for posting this. i really enjoyed reading this.

stearns and foster us

Friday, October 08, 2010 7:09 PM

stearns and foster

xhtmgealwbbwmmyhfwcl, http://donaldjpliner.org donald j pliner , DkocLPIjKATljFdOTp. bsommqiffcbrfjaecigv, http://honorar.si zaposlitev , fMrWFKSJMncMItHDak.

L�r dig Spanska us

Tuesday, October 12, 2010 10:06 AM

L�r dig Spanska

kvxahjcanyhvmfguuk, www.sprogkurser.nu/svensk/sprogkursus/spansk.html , bgUELtDxPZqgpzfwLcpc.

bodyactive leeds us

Tuesday, October 12, 2010 12:23 PM

bodyactive leeds

ktfyvbftmhdbklicrk, http://www.physique-iq.com , myUkkLmGWsgffvhkjCUL.

usp jack3d us

Tuesday, October 12, 2010 3:33 PM

usp jack3d

rntqzbuuyyowszwokd, http://www.usp-jack3d.com , MMJerFxdSlZDkfweCbLm.

Emma Roberts HD HD Wallpaper us

Thursday, October 14, 2010 8:39 PM

Emma Roberts HD HD Wallpaper

I liked this article

Spiritual forum us

Thursday, October 14, 2010 10:06 PM

Spiritual forum

Thanks for posting this. i really enjoyed reading this.

List of yoga supplies all you need to practice yoga us

Thursday, October 14, 2010 10:58 PM

List of yoga supplies all you need to practice yoga

Where is your rss? I cant find it

maximuscle cyclone review us

Saturday, October 16, 2010 5:20 PM

maximuscle cyclone review

drruaylsjxlpxxlroa, http://www.buy-sports-supplements.com , TgCQDLTCiOqqJUqUyysT.

Cross cutting shredder us

Monday, October 25, 2010 1:45 AM

Cross cutting shredder

Please post some more of this =)

Cat clips 36 and tipi makes three us

Monday, October 25, 2010 9:40 AM

Cat clips 36 and tipi makes three

This site is great. i visit here evaryday.

Blue Winter HD Wallpaper us

Tuesday, October 26, 2010 1:35 AM

Blue Winter HD Wallpaper

Hey check out my blog too. I hope i have some interesting stuff too

Emo us

Tuesday, October 26, 2010 11:47 PM

Emo

Hey check out my blog too. I hope i have some cool stuff too

Compact refrigerator us

Wednesday, October 27, 2010 9:42 PM

Compact refrigerator

This site is great. i visit here evaryday.

Cross cutting shredder us

Thursday, October 28, 2010 7:34 AM

Cross cutting shredder

Great site design!!!! Whattheme did you use?

The illustrated man us

Thursday, October 28, 2010 5:55 PM

The illustrated man

Where can i find your rss? I cant find it

Yves saint laurent parisienne gift set parisienne gift set

Thanks for posting this. i really had good time reading this.

Ireland single women at ireland dating online service

Where is your rss? I cant find it

Cross cutting shredder us

Sunday, November 07, 2010 5:57 AM

Cross cutting shredder

Hey check out my blog too. I hope i have some fun stuff too

Lancome blush subtil delicate oil free powder blush mosaique tawny

This site is cool. i visit here evaryday.

Courtney us

Sunday, November 07, 2010 4:26 PM

Courtney

Hey check out my blog too. I hope i have some fun stuff too

Maroon 5 misery by www guitartutee com us

Sunday, November 07, 2010 7:52 PM

Maroon 5 misery by www guitartutee com

Great site design!!!! Whattemplate did you use?

Jarah Mariano 11 HD Wallpaper us

Sunday, November 07, 2010 10:49 PM

Jarah Mariano 11 HD Wallpaper

Hey check out my blog too. I hope i have some fun stuff too

Facebook statuses us

Monday, November 08, 2010 1:34 AM

Facebook statuses

I liked this post

Emo us

Monday, November 08, 2010 4:26 AM

Emo

I liked this article

Compact refrigerator us

Monday, November 08, 2010 7:14 AM

Compact refrigerator

Thanks for posting this. i really enjoyed reading this.

Cross cutting shredder us

Monday, November 08, 2010 10:00 AM

Cross cutting shredder

Where can i find your rss? I cant find it

2010 Chevrolet Camaro SS 3 HD Wallpaper us

Monday, December 06, 2010 8:04 AM

2010 Chevrolet Camaro SS 3 HD Wallpaper

Where is your rss? I cant find it

Facebook statuses us

Monday, December 06, 2010 3:44 PM

Facebook statuses

I loved this article

Facebook statuses us

Monday, December 06, 2010 8:10 PM

Facebook statuses

Thanks for posting this. i really enjoyed reading this.

Emo us

Tuesday, December 07, 2010 3:13 AM

Emo

Hey check out my blog too. I hope i have some fun stuff too

Compact refrigerator us

Tuesday, December 07, 2010 8:53 AM

Compact refrigerator

This site is great. i visit here evaryday.

Compact refrigerator us

Tuesday, December 07, 2010 7:08 PM

Compact refrigerator

Thanks for posting this. i really enjoyed reading this.

Cross cutting shredder us

Wednesday, December 08, 2010 2:25 AM

Cross cutting shredder

This site is great. i visit here evaryday.

Remington s 9400 tstudio collection digital ceramic hair straightener with tourmaline 2 14 wide plates

Its a pity you dont have a donate button, i would donate some =)

2gb usb golden ingot us

Wednesday, December 08, 2010 7:31 PM

2gb usb golden ingot

Where can i find your rss? I cant find it

Flycamone2 12v usb car charger us

Thursday, December 09, 2010 2:28 AM

Flycamone2 12v usb car charger

Thanks for posting this. i really had good time reading this.

The male brain us

Thursday, December 09, 2010 8:41 AM

The male brain

Where can i find your rss? I cant find it

Dating and flirting tips dating mistakes women make when on a date

Its a pity you dont have a donate button, i would donate some =)

Preventing online poker tells us

Thursday, December 09, 2010 10:04 PM

Preventing online poker tells

Great site design!!!! Whattheme did you use?

Psychiatrypsychology in schools us

Friday, December 10, 2010 3:53 AM

Psychiatrypsychology in schools

Where can i find your rss? I cant find it

Facebook statuses us

Monday, December 13, 2010 2:26 AM

Facebook statuses

Hey check out my blog too. I hope i have some interesting stuff too

Emo us

Monday, December 13, 2010 10:33 AM

Emo

I loved this post

Compact refrigerator us

Monday, December 13, 2010 9:05 PM

Compact refrigerator

Its a pity you dont have a donate button, i would donate some =)

Cross cutting shredder us

Tuesday, December 14, 2010 5:46 AM

Cross cutting shredder

Great site design!!!! Whattemplate did you use?

Dream cheeky usb mr perfect us

Tuesday, December 14, 2010 2:52 PM

Dream cheeky usb mr perfect

Its a pity you dont have a donate button, i would donate some =)

Kathmandu pokhara jomsom 8 days us

Saturday, December 25, 2010 3:46 PM

Kathmandu pokhara jomsom 8 days

This site is great. i visit here evaryday.

Karma facebook statuses us

Sunday, December 26, 2010 12:19 AM

Karma facebook statuses

Where is your rss? I cant find it

Dora knows your name us

Sunday, December 26, 2010 10:28 AM

Dora knows your name

Hey check out my blog too. I hope i have some fun stuff too

Seize the night us

Sunday, December 26, 2010 5:45 PM

Seize the night

Its a pity you dont have a donate button, i would donate some =)

Prophecy of swords us

Sunday, December 26, 2010 7:00 PM

Prophecy of swords

I loved this article