.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

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 2010

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 (16) | Post RSSRSS comment feed

Related posts

Comments

easy candy recipe us

Monday, September 14, 2009 3:24 AM

easy candy recipe

This is the best post on this topic i have ever read.


Regards

Pitter

washington divorce us

Monday, September 14, 2009 8:03 AM

washington divorce

I posted your article to my myspace profile.


Regards

Jenki

tv mobile phone us

Saturday, October 10, 2009 2:39 AM

tv mobile phone

Hi nice site design


Regards

Cook

search engine marketing training us

Tuesday, November 17, 2009 1:52 AM

search engine marketing training

Nice information need to know more.


Regards
Rohr


designer bags sale US

Tuesday, December 01, 2009 5:08 PM

designer bags sale

What a great info, thank you for sharing. this will help me so much in my learning.

supra sneakers US

Thursday, December 24, 2009 3:52 PM

supra sneakers

While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find. I do appreciate that youve added relevant and intelligent commentary here though. Thank you!

discount designer handbags US

Wednesday, December 30, 2009 6:56 AM

discount designer handbags

If you need to buy discount gucci bags, you can just type bagsvendor.com in you browser address line and find out the gucci bags on big sale in discounted price. The information below is only introducing the discount news about gucci bags and gucci handbags. A woman leads the world accounting her gucci bag. But then we must also remember that there is a well discount news that has to see. It is a common complaint that women having their share is not large enough to hold all your belongings necessary, such as discount gucci bags, designer bags. This gucci bags you can find in bagsvendor.com is that the person is unable to organize things properly. Each discount bag has a clear purpose and objects you choose to take the bag must be compatible with the purposes for which it is purchased in your mind. We can meet the wide range of needs that are part of the common things for a trip on a beautiful handbag, bags or purses. Thus, while the discounted gucci bags needs, here are some points that may be useful to organize the contents of the discount gucci bag. Even it is a discounted price gucci bags, but it has high quality. The quality of materials used with the gucci bag is leather, suede or cotton. These materials can be sure that they are not less than 100% authentic and of excellent quality discount bags. Accessories and decorative items from the discount gucci bag, such as hinges, chains, rings and belt buckles are higher that other bags. Some designers in the Gucci company will even give as a repair or replacement free of charge in a period of time to ensure that their discount handbags are offered in a different league than the shares that you can find at any mall or online store. Designer Gucci bags can be changed to discounted gucci bags. This is a big news about handbags on sale. Brands are just called by many famous fashion people, and can be done with any discounted news. Just find more in bagsvendor.com

air jordan US

Friday, January 01, 2010 2:51 AM

air jordan

In winnersneaker, these Nike Air Jordan shoes were associated with sport, fashion and basketball. The Nike Air Jordan sneakers were a way to hide some of the smaller sizes, or raise an important NBA star to even higher levels to the count. Nike Air Jordan shoes, but also less fashionable of Air Jordan history. Which were commonly used by high-ranking customers in NBA during the 19th century and were used in the 20th century to let the putrid mud in the streets, all you know is that you can find many discounted Air Jordan shoes in winnersneaker.com. Existing Air Jordan retro sneakers in the United States and Europe retails do not feel in fashion until the new Air Jordan shoes come in the street. At first the Air Jordan 1 shoes important wearing for young sport men, but once reigned basketball, Jordan shoes became the must-have fashion accessory for young people in the basketball playground. These Air Jordan original shoes were all to make a statement vividly. Air Jordans like Air Jordan 23 and Air Jordan 6 wore basketball shoes kiss in the context of his people larger than life. Wish you find a suitable Air Jordan shoes in winnersneaker.com.

alta white teeth whitening us

Friday, February 19, 2010 11:28 AM

alta white teeth whitening

The optimist sees opportunity in every danger; the pessimist sees danger in every opportunity.

discount handbags sale US

Sunday, March 07, 2010 4:04 PM

discount handbags sale

Substantially, the article is in reality the sweetest on this precious topic. I harmonise with your conclusions and will thirstily look forward to your upcoming updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing. I will directly grab your rss feed to stay abreast of any updates. Pleasant work and much success in your business efforts!

discount bag US

Monday, March 08, 2010 10:54 PM

discount bag

Your blog is perfect, and I like this article. I find the information I need. I think I can find more useful information here, thanks.

designer handbags sale US

Tuesday, March 09, 2010 2:33 PM

designer handbags sale

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.

Lebron shoes US

Wednesday, March 10, 2010 9:44 PM

Lebron shoes

While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find. I do appreciate that youve added relevant and intelligent commentary here though. Thank you!

wholesale shoes from china US

Wednesday, March 10, 2010 11:28 PM

wholesale shoes from china

You did the a great work writing and revealing the hidden beneficial features of BlogEngine.Net, that I found it was popularly used by bloggers nowadays. I think BE has emerged to be one of the best blogging platform right now. I wish you good luck with your blogging experiences.

discount bags US

Thursday, March 11, 2010 2:47 AM

discount bags

I find your blog in google. And I will be back next time, thanks.

basketball sheos US

Thursday, March 11, 2010 6:47 AM

basketball sheos

Hey, guy, your blog is nice. It can bring me many useful information.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Thursday, March 11, 2010 9:49 PM