简体   繁体   English

在IE中从Javascript调用Silverlight代码

[英]Call Silverlight code from Javascript in IE

I have a Silverlight application in an HTML page. 我在HTML页面中有一个Silverlight应用程序。 The SL plugin is hosted in an "object" HTML tag. SL插件托管在“对象”HTML标记中。 When the user closes the Web page, I want to call a function inside my Silverlight application. 当用户关闭网页时,我想在Silverlight应用程序中调用一个函数。 The code is something like this: 代码是这样的:

The (simplified) HTML code: (简化)HTML代码:

<div id="silverlightControlHost">
    <object id="MyApp" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
        <param name="source" value="ClientBin/MyApp.xap" />
        ...
        </object><iframe id="_sl_historyFrame"></iframe></div>

The Javascript code: Javascript代码:

<script type="text/javascript">
    window.onbeforeunload = confirmExit;

    function confirmExit()
    {
        var control = document.getElementById("MyApp");
        var message = control.content.BrowserIntegration.MyAppFunction();

        if (message)
        {
            return message;
        }
    }
</script>

It used to work and it doesn't work anymore, at least in Internet Explorer 8. The content property of the HTML object (control.content) is undefined. 它曾经工作,它不再起作用,至少在Internet Explorer 8中.HTML对象的内容属性(control.content)是未定义的。 Strange. 奇怪。 Has there been a change recently for this property? 最近这个房产有变化吗? I am pretty sure that it worked 2 months ago, and that it has worked well for a long time before that. 我很确定它在2个月前有效,并且在此之前它已经运行了很长时间。 The Web site is in my trusted sites and I haven't found a setting in IE that I have changed since then (there could be one or two but, scanning the list, I haven't found anything). 该网站是在我信任的网站,我没有在IE中找到我从那时起改变的设置(可能有一个或两个,但扫描列表,我还没有找到任何东西)。

When I make a test with the "onLoad" event of the HTML object, the content is already undefined. 当我使用HTML对象的“onLoad”事件进行测试时,内容已经是未定义的。

I don't know if the anomaly is the fact that it worked before or that it doesn't work now. 我不知道异常是否是它以前工作的事实,或者它现在不起作用。 But if somebody can tell me how to make it work now, it will make my day. 但如果有人能告诉我如何让它现在发挥作用,它将成为我的一天。

I create some wizard for calling Silverlight code from IE: 我创建了一些从IE调用Silverlight代码的向导:

1)Need identify a silverlight control: add "id" tag: 1)需要识别silverlight控件:添加“id”标签:

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
            width="100%" height="100%" id="_sl_facebookapp">

2) Need register scriptable object from silverlight code: 2)需要从silverlight代码注册脚本化对象:

HtmlPage.RegisterScriptableObject("FBSHandler", this);

3) Mark needed silverlight method as ScriptableMember: 3)Mark需要silverlight方法作为ScriptableMember:

[ScriptableMember]
        public void FBAuthorized(string authKey){}

4) You should use registered scriptable object for calling Silverlight method: 4)您应该使用已注册的脚本对象来调用Silverlight方法:

var silverlightCtrlId = '_sl_facebookapp';

function handleStatusResponse(response) {
    var control = document.getElementById(silverlightCtrlId);    
        var accessToken = response.authResponse.accessToken;
        if (control != null) {
            control.Content.FBSHandler.FBAuthorized(accessToken);
        }
    }
}

It works fine. 它工作正常。 Hope it will help you. 希望它会对你有所帮助。

These operations did not solve my problem: 这些操作没有解决我的问题:

  • Detecting and removing viruses 检测和删除病毒
  • Lowering the security level in IE8 降低IE8中的安全级别
  • Reinstalling Internet Explorer 8 重新安装Internet Explorer 8
  • Reinstalling the Silverlight component 重新安装Silverlight组件

This is what solved my problem: 这就解决了我的问题:

  • Reinstalling the Silverlight Developer runtime 重新安装Silverlight Developer运行时

Note: I use MS Visual Studio 2010 for .NET development 注意:我使用MS Visual Studio 2010进行.NET开发

I think this is a timing issue. 我认为这是一个时间问题。 Your code to access the Content element may be executing before the Silverlight Component is entirely loaded. 在完全加载Silverlight组件之前,可能正在执行访问Content元素的代码。

I had the exact same issue and resolved it by using Javascript Timer till the element was completely loaded. 我有完全相同的问题,并通过使用Javascript计时器解决它,直到元素完全加载。 Interestingly the issue was only with IE and none of the other browsers. 有趣的是,这个问题只适用于IE而不是其他浏览器。

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

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