如何调用javascript函数并从XAML / Silverlight传递值?

时间:2021-05-20 20:44:51

I'm creating silverlight without Visual Studio. I just have raw html, XAML, and js (javascript).

我在没有Visual Studio的情况下创建了silverlight。我只有原始的html,XAML和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.

我想要做的是将值从XAML传递给javascript。我可以从XAML调用并激活javascript函数。见下文。 canvas元素有一个鼠标左按钮事件,它在javascript中调用LandOnSpace。

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");

但是我怎么称呼ShowMsg?或者更确切地说,我如何将值传递给该调用?通常在javascript中你可以去:ShowMsg(500,700,“你欠我们钱”);

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

但是当我在xaml代码中尝试时,它会破坏某些东西。我相信它抱怨javascript函数不存在。

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

2 个解决方案

#1


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

如果要从Silverlight 2.0调用javascript函数,可以在System.Windows.Browser命名空间中使用HtmlPage。

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.

但是,基于上面的示例,您似乎正在使用Silverlight 1.0,其中您的事件处理程序在Javascript中,而不是在C#或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.

您可以转到Silverlight 2.0。用于服务器页面的服务器不会阻止您使用Silverlight 2.0(或3.0)。您可以在Google App Engine上完美运行Silverlight 2.0应用程序。

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

要开始在2.0中开发,请在此处下载Silverlight 2工具:http://www.microsoft.com/downloadS/details.aspx?familyyid = 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

有关如何在Silverlight托管代码和浏览器中的Javascript之间进行通信的一些参考,您可以查看此页面:http://msdn.microsoft.com/en-us/library/cc645076(VS.95).aspx

#2


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.

正确的方法是使用默认参数触发js处理程序。然后,从Javascript,使用Silverlight 1.0模型导航到XAML。请参阅Silverlight 1.0文档。请参见FindName方法。

#1


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

如果要从Silverlight 2.0调用javascript函数,可以在System.Windows.Browser命名空间中使用HtmlPage。

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.

但是,基于上面的示例,您似乎正在使用Silverlight 1.0,其中您的事件处理程序在Javascript中,而不是在C#或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.

您可以转到Silverlight 2.0。用于服务器页面的服务器不会阻止您使用Silverlight 2.0(或3.0)。您可以在Google App Engine上完美运行Silverlight 2.0应用程序。

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

要开始在2.0中开发,请在此处下载Silverlight 2工具:http://www.microsoft.com/downloadS/details.aspx?familyyid = 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

有关如何在Silverlight托管代码和浏览器中的Javascript之间进行通信的一些参考,您可以查看此页面:http://msdn.microsoft.com/en-us/library/cc645076(VS.95).aspx

#2


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.

正确的方法是使用默认参数触发js处理程序。然后,从Javascript,使用Silverlight 1.0模型导航到XAML。请参阅Silverlight 1.0文档。请参见FindName方法。