Communicate Between Flex and Silverlight using Javascript
| You're probably asking yourself, why would I ever want to do that? To be honest, we haven't thought of a great reason yet. Maybe Silverlight can do something Flex can't, or Flex has a feature Silverlight doesn't, and you want combine both into one all-powerful web page. Well now you can. |
|
Depending on whether or not you have all the required plugins installed, you should see an example of Flex/Silverlight communication on the right. The sliders on the top and left were created using Flex. The sphere in the middle was created using Silverlight. When you drag the sliders, the sphere will roll around accordingly. You can also click and drag the sphere, which will move the sliders. |
|
If you can't see the above example, it either means you don't have Flash 9 or the Silverlight 1.1 Alpha Refresh Runtime.
This tutorial isn't going to go into the specifics of how Silverlight and Flex communication with Javascript. For detailed information on how to accomplish that, check out our previous two tutorials: Silverlight and Javascript Interaction and Flex & Javascript Tutorial - Simple Interaction.
We'll start by looking at what happens when one of the sliders is moved.
function MoveBallX(val)
{
var silverlightControl =
document.getElementById('SilverlightControl');
silverlightControl.Content.SilverlightApp.MoveBallX(val);
}
function MoveBallY(val)
{
var silverlightControl =
document.getElementById('SilverlightControl');
silverlightControl.Content.SilverlightApp.MoveBallY(val);
}
{
var silverlightControl =
document.getElementById('SilverlightControl');
silverlightControl.Content.SilverlightApp.MoveBallX(val);
}
function MoveBallY(val)
{
var silverlightControl =
document.getElementById('SilverlightControl');
silverlightControl.Content.SilverlightApp.MoveBallY(val);
}
When the horizontal slider is moved, the Flex application calls the Javascript function
MoveBallX and passes in the current value of the slider. This function then calls the C# function MoveBallX in the Silverlight application. Silverlight then positions the ball to the new X position. The same thing happens when the vertical slider is moved, but now MoveBallY is called. That is all the Javascript code required to move the sphere when one of the Flex sliders is moved.
Now let's look at what happens when the drags the sphere.
function xChanged()
{
var silverlightControl =
document.getElementById('SilverlightControl');
xPos = silverlightControl .Content.SilverlightApp.GetXValue();
getFlexApp('HorizontalSilverlightMover').setXValue(val);
}
function yChanged()
{
var silverlightControl =
document.getElementById('SilverlightControl');
yPos = silverlightControl.Content.SilverlightApp.GetYValue();
getFlexApp('VerticalSilverlightMover').setYValue(val);
}
function getFlexApp(appName) {
if (navigator.appName.indexOf ("Microsoft") !=-1) {
return window[appName];
} else {
return document[appName];
}
}
{
var silverlightControl =
document.getElementById('SilverlightControl');
xPos = silverlightControl .Content.SilverlightApp.GetXValue();
getFlexApp('HorizontalSilverlightMover').setXValue(val);
}
function yChanged()
{
var silverlightControl =
document.getElementById('SilverlightControl');
yPos = silverlightControl.Content.SilverlightApp.GetYValue();
getFlexApp('VerticalSilverlightMover').setYValue(val);
}
function getFlexApp(appName) {
if (navigator.appName.indexOf ("Microsoft") !=-1) {
return window[appName];
} else {
return document[appName];
}
}
Again, there's not a lot of Javascript code required to keep the two applications synchronized. The functions
xChanged and yChanged are hooked up to events in the Silverlight application and are called whenever a user moves the ball in either the X or Y direction. The first thing these functions do is get the new position from the Silverlight application by calling either GetXValue or GetYValue. It then simply calls setXValue or setYValue to set the horizontal or vertical slider to its new position. The function getFlexApp is explained in details in the earlier Flex and Javascript tutorial, but it basically acquires a reference to the Flex application depending on what type of browser is currently being used.
As you can see, the actual code to do Flex/Silverlight communication is very simple. This tutorial was meant to let everyone know that the possibility existed. As a reminder, please refer to Silverlight and Javascript Interaction for detailed information on how to communicate between Silverlight and Javascript. Also refer to Flex & Javascript Tutorial - Simple Interaction for information on how to communicate between Flex and Javascript.
If we didn't make anything clear enough, let us know in the comments.
Posted in Silverlight, Flex, Javascript, All Tutorials by The Reddest |

August 20th, 2007 at 11:28 am
Love it!
Yes.. keep more coming…
–
Scott Barnes
Developer Evangelist
Microsoft.
August 27th, 2007 at 5:21 am
gud stuff. but anything is there that will help me for communicate between html tags in a page with other php page?
September 3rd, 2007 at 3:43 am
Here is a reason for SL-Flex interaction:
SL has no client side microphone and webcam support, while Flex does have. So if you want to code mainly in C#, but use the mic and webcam you will have to use both technologies.
If someone could provide a sample I will be very thankfull.
September 5th, 2007 at 8:29 am
No offense guys, but I do have Flash 9 and I do have the latest Silverlight, and in neither IE nor Firefox I get to see your example. Each and everytime I do get to see a huge “Install Silverlight” button, which I then click, and then Microsoft thanks me for having Silverlight installed.
(I’m working on a VMWare WinXP which was originally a Microsoft Virtual PC image, perhaps that’s the problem?)
September 5th, 2007 at 8:41 am
That is not the latest Silverlight if you just install from the button. You also have to install the Alpha 1 refresh, over at http://msdn2.microsoft.com/en-us/silverlight/bb419317.aspx.
Try that and let us know if you get any other issues.
March 13th, 2008 at 3:02 am
Hi,
I installed Silverlight 1.1 Alpha from the link you provided. I don’t get the missing Silverlight message but a popup indicating a ParseError (errorCode 1001).
Message : AG_E_UNKNOWN_ERROR
XamlFile: /files/Tutorials/Silverlight/Flex_Interaction/page.xaml
Line: 9
Position: 9
What’s the problem ?
Thanks a lot.
May 27th, 2008 at 10:38 pm
John: I have a basic Silverlight 2 Webcam example, using flash, at http://jonas.follesoe.no/PermaLink,guid,7f8857dc-5f37-4a8a-bd35-fd4b624af45b.aspx