I'm attempting to call a javascript function (in our code) from a silverlight control. I'm attempting to call the function via:
我试图从silverlight控件调用一个javascript函数(在我们的代码中)。我试图通过以下方式调用该函数:
HtmlPage.Window.Invoke("showPopup", new string[] { "http://www.example.com" });
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"});
without issue, but not my own function.
我可以调用HtmlPage.Window.Invoke(“alert”,new string [] {“test”});没有问题,但不是我自己的功能。
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. What am I missing here?
所以js函数工作,Silverlight二进制文件可以找到其他js函数。我在这里想念的是什么?
Additional Notes:
- The function call is in a button click event handler, so it happens after the page (and the script) have been loaded)
函数调用在按钮单击事件处理程序中,因此它在页面(和脚本)加载后发生)
6 个解决方案
#1
Is the showPopup javascript function on the same html or aspx page as the Silverlight control? You will normally get the "Failed to Invoke ..." error if the javascript function does not exist:
showPopup javascript函数与Silverlight控件位于同一个html或aspx页面上吗?如果javascript函数不存在,通常会出现“Failed to Invoke ...”错误:
HtmlPage.Window.Invoke("functionThatDoesNotExist", new [] { "Testing" });
alt text http://www.freeimagehosting.net/uploads/3ed38e3ce8.png
alt text http://www.freeimagehosting.net/uploads/3ed38e3ce8.png
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...
是否可以列出一个简短但完整的程序代码,导致在您的机器上发生此问题...
#2
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. 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>
. Adding a reference to the file in the in-the-iframe <head>
solved the problem.
有问题的Silverlight控件在iframe中。问题是包含showPopup函数的文件在外部中被引用(为什么我可以使用IE工具栏调用该函数)而不是内部。在in-the-iframe 中添加对文件的引用解决了问题。
Sort of anticlimactic, but thanks for all the help.
有点虎头蛇尾,但感谢所有的帮助。
#3
Actually referencing the script again from the iframe is not the most efficient way to reference code contained in the parent. If your function is called "showPopup", you can insert this in your iframe:
实际上,从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 (e.g parent.showPopup('....')) or declare a local alias for it (which is what we do in the above example).
瞧。对此的解释是所有“全局”函数和对象都是这个“全局命名空间”的一部分......这是“窗口”对象。因此,如果您尝试从子级访问“全局”函数,则需要在父级上调用该函数(例如,parent.showPopup('....'))或为其声明一个本地别名(这是我们在上面的例子中做了什么)。
Cheers!
#4
Make sure your script is fully loaded before trying to invoke functions from it.
在尝试从中调用函数之前,请确保您的脚本已完全加载。
#5
Here's how I do it. But I'm creating silverlight without visual studio. I just have raw html, xaml, and js (javascript). Notice MouseLeftButtonUp and it's value "LandOnSpace"
我就是这样做的。但我在没有视觉工作室的情况下创造了银光。我只有原始的html,xaml和js(javascript)。注意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.');
};
}
#6
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. However this file had not been added to the head section of the ASPX file. 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.
我在使用SL 4的VS 2010中遇到了同样的问题。我创建了一些方法并将它们放入一个单独的JS文件中。但是,此文件尚未添加到ASPX文件的head部分。添加它解决了这个问题。不同之处在于虽然我在iframe中没有单独的头部,但我遇到了问题并且它已经解决了。
#1
Is the showPopup javascript function on the same html or aspx page as the Silverlight control? You will normally get the "Failed to Invoke ..." error if the javascript function does not exist:
showPopup javascript函数与Silverlight控件位于同一个html或aspx页面上吗?如果javascript函数不存在,通常会出现“Failed to Invoke ...”错误:
HtmlPage.Window.Invoke("functionThatDoesNotExist", new [] { "Testing" });
alt text http://www.freeimagehosting.net/uploads/3ed38e3ce8.png
alt text http://www.freeimagehosting.net/uploads/3ed38e3ce8.png
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...
是否可以列出一个简短但完整的程序代码,导致在您的机器上发生此问题...
#2
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. 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>
. Adding a reference to the file in the in-the-iframe <head>
solved the problem.
有问题的Silverlight控件在iframe中。问题是包含showPopup函数的文件在外部中被引用(为什么我可以使用IE工具栏调用该函数)而不是内部。在in-the-iframe 中添加对文件的引用解决了问题。
Sort of anticlimactic, but thanks for all the help.
有点虎头蛇尾,但感谢所有的帮助。
#3
Actually referencing the script again from the iframe is not the most efficient way to reference code contained in the parent. If your function is called "showPopup", you can insert this in your iframe:
实际上,从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 (e.g parent.showPopup('....')) or declare a local alias for it (which is what we do in the above example).
瞧。对此的解释是所有“全局”函数和对象都是这个“全局命名空间”的一部分......这是“窗口”对象。因此,如果您尝试从子级访问“全局”函数,则需要在父级上调用该函数(例如,parent.showPopup('....'))或为其声明一个本地别名(这是我们在上面的例子中做了什么)。
Cheers!
#4
Make sure your script is fully loaded before trying to invoke functions from it.
在尝试从中调用函数之前,请确保您的脚本已完全加载。
#5
Here's how I do it. But I'm creating silverlight without visual studio. I just have raw html, xaml, and js (javascript). Notice MouseLeftButtonUp and it's value "LandOnSpace"
我就是这样做的。但我在没有视觉工作室的情况下创造了银光。我只有原始的html,xaml和js(javascript)。注意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.');
};
}
#6
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. However this file had not been added to the head section of the ASPX file. 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.
我在使用SL 4的VS 2010中遇到了同样的问题。我创建了一些方法并将它们放入一个单独的JS文件中。但是,此文件尚未添加到ASPX文件的head部分。添加它解决了这个问题。不同之处在于虽然我在iframe中没有单独的头部,但我遇到了问题并且它已经解决了。