简体   繁体   English

准备就绪时,从Javascript调用Silverlight方法

[英]Call Silverlight method from Javascript when it's ready

I have a page that loads another window on button click. 我有一个页面,单击按钮会加载另一个窗口。 The loaded page has silverlight control on it, so it takes some time to load and get prepared before it can receive javascript calls. 加载的页面上具有Silverlight控件,因此加载和准备需要一些时间才能接收JavaScript调用。

What I need to do is to call a particular method of silverlight object right after the silverlight plugin gets loaded and is ready to interact with me. 我需要做的是在silverlight插件加载好并准备与我互动之后立即调用silverlight对象的特定方法。

Now, if the pop-up page was already opened then the code would be like that: 现在,如果已经打开了弹出页面,则代码将如下所示:

var slWin = window.open('PopupPage.html', 'WindowName');
var elem = slWin.document.getElementById('slControl');
elem.Content.SlObject.MethodA();

This works when the window is already opened because the control is already loaded and ready. 当窗口已经打开,因为控件已被加载并准备就绪,这将起作用。 I need to modify this code to handle the situation when the elem need some time to be prepared. elem需要一些时间准备时,我需要修改此代码以处理这种情况。

I tried to use jQuery's ready and load methods to add handlers to corresponding events, but with no particular lack. 我尝试使用jQuery的readyload方法将处理程序添加到相应的事件中,但没有特别的不足。 Here's the full snippet: 这是完整的代码段:

var slWin = window.open('', 'WindowName');

var elem = slWin.document.getElementById('slControl');
if (elem == null) {
    slWin.location.href = 'PopupPage.aspx';

    // this branch doesn't work
    $(slWin).load(function () {

        elem = slWin.document.getElementById('slControl');
        elem.Content.SlObject.MethodA();
    });
}
else {
    // this branch works fine
    elem.Content.SlObject.MethodA();
}

How do I solve this issue? 我该如何解决这个问题? I don't mind jQuery solutions. 我不介意jQuery解决方案。

This error is happening because the Silverlight object is not fully loaded when you are trying to access it. 发生此错误的原因是,当您尝试访问Silverlight对象时,该对象未完全加载。

Try to use the "onload" event of the silverlight object to dectect when it's ready to use. 当准备使用时,尝试使用silverlight对象的“ onload”事件进行检测。 Here you have a link to the MSDN documentation: 在这里,您具有MSDN文档的链接:

http://msdn.microsoft.com/en-us/library/cc838107(v=vs.95).aspx http://msdn.microsoft.com/zh-CN/library/cc838107(v=vs.95).aspx

Hope it helps. 希望能帮助到你。 :) :)

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

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