简体   繁体   English

从IFrame中托管的Silverlight调用JavaScript

[英]Call JavaScript from Silverlight hosted in IFrame

I am developing a Silverlight application which is going to be hosted inside another's application IFrame: 我正在开发一个Silverlight应用程序,该应用程序将托管在另一个应用程序的IFrame中:

<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
<script type="text/javascript">
    function testMethod() {
        alert(1);
    }       
</script>
<iframe src="URL of the silverlight application" width="750" height="500"/>

This is just a dummy app for testing purposes (I am using the default MVC 4 template - this is actuly the Contacts.cshtml file) 这只是一个用于测试目的的虚拟应用程序(我正在使用默认的MVC 4模板-实际上是Contacts.cshtml文件)

An this is the part where I call the JavaScript function. 这是我调用JavaScript函数的部分。

HtmlPage.Window.Invoke("testMethod");

Any suggestions what to do/what I am doing wrong? 有什么建议怎么办/我在做什么错?

Try:- 尝试:-

ScriptObject parent = HtmlPage.Window.GetProperty("parent") as ScriptObject;
if (parent != null)
{
     parent.Invoke("testMethod");
}

This is what has worked for me in this case 在这种情况下,这就是对我有用的

HtmlPage.Window.Eval("if(typeof window.parent.testMethod== 'function'){window.parent.testMethod()};");

I know that .Eval should be avoided but it looks like it's the only way for me :/ 我知道应该避免.Eval但看来这是我的唯一方法:/

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

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