1

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 calls 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;   } 

3
  • Considering all the issues, would it be beneficial for me to switch to moonlight instead of silverlight? I might also abandon silverlight altogether and use google's O3D for rendering. It can handle the 2d stuff I want to do, quite easily. Commented Jul 6, 2009 at 16:04
  • I have also been looking at using the Canvas tag from HTML 5. Then my browser support will be every browser. IE supports it apparently, one way or another. Commented Jul 6, 2009 at 17:38
  • This is interesting: blog.jimmy.schementi.com/2008/05/… There is something called chiron that might help me out. Commented Jul 15, 2009 at 18:16

2 Answers 2

2

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&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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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.

4 Comments

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.
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 ;)
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.