简体   繁体   English

从Silverlight调用Javascript函数

[英]Calling A Javascript function from Silverlight

I'm attempting to call a javascript function (in our code) from a silverlight control. 我试图从silverlight控件调用一个javascript函数(在我们的代码中)。 I'm attempting to call the function via: 我试图通过以下方式调用该函数:

HtmlPage.Window.Invoke("showPopup", new string[] { "http://www.example.com" });

and I get the error "Failed to Invoke: showPopup" 我收到错误“无法调用:showPopup”

I can call HtmlPage.Window.Invoke("alert", new string[]{"test"}); 我可以调用HtmlPage.Window.Invoke("alert", new string[]{"test"}); without issue, but not my own function. 没有问题,但不是我自己的功能。

I can also open up the page in question in the IE developer tools and manually call showPopup("http://www.example.com") and it works as expected. 我还可以在IE开发人员工具中打开相关页面并手动调用showPopup("http://www.example.com") ,它可以按预期工作。

So the js function works, and the Silverlight binary can find other js functions. 所以js函数工作,Silverlight二进制文件可以找到其他js函数。 What am I missing here? 我在这里错过了什么?

Additional Notes: 补充说明:

  • The function call is in a button click event handler, so it happens after the page (and the script) have been loaded) 函数调用在按钮单击事件处理程序中,因此它在页面(和脚本)加载后发生)

Is the showPopup javascript function on the same html or aspx page as the Silverlight control? showPopup javascript函数与Silverlight控件位于同一个html或aspx页面上吗? You will normally get the "Failed to Invoke ..." error if the javascript function does not exist: 如果javascript函数不存在,通常会出现“Failed to Invoke ...”错误:

HtmlPage.Window.Invoke("functionThatDoesNotExist", new [] { "Testing" });

替代文字

What browser are you using when you are getting this problem? 当你遇到这个问题时,你使用什么浏览器?

Are you using the latest version of Silverlight? 您使用的是最新版本的Silverlight吗?

Are you using the ScriptableType attrbiute anywhere? 您是否在任何地方使用ScriptableType attrbiute?

Is it possible to list the code for a short but complete program that causes this problem to happen on your machine... 是否可以列出一个简短但完整的程序代码,导致在您的机器上发生此问题...

Aha! 啊哈! I figured it out. 我想到了。 Our app uses an iframe, so the rendered html looks something like this 我们的应用程序使用iframe,因此渲染的html看起来像这样

<html>
<head></head>
<body>
Stuff
<iframe>
    <html>
        <head></head>
        <body>Other Stuff</body>
    </html>
</iframe>
<body>
</html>

And the Silverlight control in question is in the iframe. 有问题的Silverlight控件在iframe中。 The problem was that the file that contained the showPopup function was referenced in the outer <head> (why I could call the function with the IE toolbar) but not the inner <head> . 问题是包含showPopup函数的文件在外部<head>被引用(为什么我可以使用IE工具栏调用该函数)而不是内部<head> Adding a reference to the file in the in-the-iframe <head> solved the problem. 在in-the-if​​rame <head>添加对文件的引用解决了问题。

Sort of anticlimactic, but thanks for all the help. 有点虎头蛇尾,但感谢所有的帮助。

Actually referencing the script again from the iframe is not the most efficient way to reference code contained in the parent. 实际上,从iframe再次引用脚本并不是引用父代码中包含的代码的最有效方法。 If your function is called "showPopup", you can insert this in your iframe: 如果您的函数名为“showPopup”,则可以在iframe中插入:

<script type="text/javascript">
    var showPopup = parent.showPopup;
</script>

And voilà. 瞧。 The explanation for this is that all "global" functions and objects are part of this "global namespace"... which is the "window" object. 对此的解释是所有“全局”函数和对象都是这个“全局命名空间”的一部分......这是“窗口”对象。 So if you're trying to access "global" functions from a child, you need to either call the function on the parent (eg parent.showPopup('....')) or declare a local alias for it (which is what we do in the above example). 因此,如果您尝试从子级访问“全局”函数,则需要在父级上调用该函数(例如,parent.showPopup('....'))或为其声明一个本地别名(这是我们在上面的例子中做了什么)。

Cheers! 干杯!

Here's how I do it. 这是我如何做到的。 But I'm creating silverlight without visual studio. 但我在没有视觉工作室的情况下创造了银光。 I just have raw html, xaml, and js (javascript). 我只有原始的html,xaml和js(javascript)。 Notice MouseLeftButtonUp and it's value "LandOnSpace" 注意MouseLeftButtonUp和它的值“LandOnSpace”

        <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.');
};

} }

I had the same problem in VS 2010 with SL 4. I had created a few methods and put them into one single JS file. 我在使用SL 4的VS 2010中遇到了同样的问题。我创建了一些方法并将它们放入一个单独的JS文件中。 However this file had not been added to the head section of the ASPX file. 但是,此文件尚未添加到ASPX文件的head部分。 Adding it solved the problem. 添加它解决了这个问题。 The difference is that though I did not have a separate head section in the iframe, I had the problem and it got solved. 不同之处在于虽然我在iframe中没有单独的头部,但我遇到了问题而且它已经解决了。

在尝试从中调用函数之前,请确保您的脚本已完全加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM