views:

2021

answers:

2

I'm creating silverlight without visual studio. I just have raw html, xaml, and js (javascript).

What I want to do is pass values from the xaml to the javascript. I can call and activate javascript functions from xaml. See below. The canvas element has a mouse left button up event that callls LandOnSpace in the javascript.

But how would I call ShowMsg? Or more accurately, how would I pass values to that call? Normally in javascript you can just go: ShowMsg(500, 700, "you owe us money");

But when I try that in the xaml code, it breaks something. I believe it complains that the javascript function doesn't exist.

  <Canvas x:Name="btnLandOnSpace" Background="LightGreen" MouseLeftButtonUp="LandOnSpace"
   Cursor="Hand" Canvas.Top ="0"  Width="70" Height="50"> 
   <TextBlock Text="LandOnSpace"  />
   </Canvas>
function LandOnSpace(sender, e) {  //on server
if (!ShipAnimateActive && !blnWaitingOnServer) {
 blnWaitingOnServer = true;
 RunServerFunction("/sqgame/getJSLLandOnSpace");
        ShowWaitingBox();
        };
else {
 alert('Waiting on server.');
};
}



function ShowMsg(SintCost, SintRent , SstrChoiceText) { 
blnPayChoice = true;  
intCost = SintCost;   
intRent = SintRent;  
strChoiceText = SstrChoiceText;   }
+1  A: 

The right way to do this is to fire the js handler with the default parameters. Then, from Javascript, use the Silverlight 1.0 model to navigate into the XAML. See the Silverlight 1.0 docs. See the FindName method.

Erik Mork
That's what I do now. For example, I change the name of the xaml element to include the ID number it would have passed. For example, I would change x:Name="btnLandOnSpace" to x:Name="btnLandOnSpace05". And the javascript would use sender.name to get and parse the name.Is it not possible to directly pass variables though? I can think of scenarios where I'd want or need to pass another variable along with it. I suppose I could embed multiple values in the name but that seems messy.
Neo42
It's not really possible to pass the values directly to the handler, no. Especially not while using the 1.0 (JS) programming model. If you're using SL 2 with Prism Commanding, why then, that's a different story ;)
Erik Mork
Are you saying I can't even use silverlight 2 when only using JS? I could have sworn I'd implemented Silverlight 2 functionality. I'm running the page on a google app engine site, so I can't just switch to .net, unfortunately, as far as I know. I tested all the ways I could implement silverlight on a different server though. I was pretty sure I needed a .net 3.0 server to host any other kind of silverlight page. Is this right? Should I start a new question on that here?
Neo42
When you use Javascript with Silverlight, we refer to it as the Silverlight 1.0 programing model. It's available in Silverlight 1.0 and Silverlight 2 and Silverlight 3. Two things: Silverlight can run .net on the client. I highly recommend you look in this "managed" model.Silverlight doesn't require .Net on the server.
Erik Mork
+1  A: 

If you want to call javascript functions from Silverlight 2.0 you can use HtmlPage in the System.Windows.Browser namespace.

var param = new object[] {"some parameter"};
HtmlPage.Window.Invoke("myfunc",param);

However based on your example above it seems that you are using Silverlight 1.0 where your event Handler is in Javascript and not in C# or VB.

You can move to Silverlight 2.0. The server that you use to server pages doesn't prevent you from using Silverlight 2.0 (or 3.0). You can perfectly run a Silverlight 2.0 application on Google App Engine.

To start developing in 2.0 download the Silverlight 2 Tools here: http://www.microsoft.com/downloadS/details.aspx?familyid=C22D6A7B-546F-4407-8EF6-D60C8EE221ED&amp;displaylang=en

And for some reference on how to communicate between the Silverlight managed code and the Javascript inside the browser you can check this page: http://msdn.microsoft.com/en-us/library/cc645076(VS.95).aspx

Guillaume Gros
Wow. Um I have about 3000 lines of code already in my javascript file. I'll see what I can do though to start connecting it to managed code. Or at least testing out some managed code on my google app engine site. And my next project will start out that way. My code is already in 3 languages, .JS, .XAML, and Python. I'm not sure I should throw vb .net at the moment. I know vb, but the project has gone a long way without it so far. I'll experiment though at some point. Thank you.
Neo42