Reputation: 650
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; }
Upvotes: 1
Views: 6739
Reputation: 371
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
Upvotes: 2
Reputation: 1443
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.
Upvotes: 1