문제

저는 자바스크립트가 많이 사용되는 Silverlight 응용 프로그램을 만들고 있습니다.

JS 상호 작용을 활성화하기 위해 다음 SL 클래스를 만들었습니다.

[ScriptableType]
public class JavaScriptProxy
{
    private string _version;

    // provided for testing SL-JS integration
    [ScriptableMember]
    public void SmokeTest() { HtmlPage.Window.Alert("Hello world!"); }
}

그리고 이를 기본 SL 애플리케이션의 생성자에 로드했습니다.

public App()
{
    this.Startup += this.onStartup;
    this.Exit += this.Application_Exit;
    this.UnhandledException += this.Application_UnhandledException;

    InitializeComponent();

    // register javascript bridge proxy
    // (must register in constructor so proxy is available immediatly)
    HtmlPage.RegisterScriptableObject("JsProxy", new JavaScriptProxy());
}

그러나 이 앱은 자바스크립트가 많은 앱이므로 자바스크립트 자체를 통해 로드할 수 있어야 합니다.

즉.뭔가 함께:

// called on body.onLoad
function init() {

    var proxy;

    var el = document.getElementById("target_canvas");

    Silverlight.createObject(..., el, "agApp" ..., {

        onLoad: function() {
            proxy = agApp.Content.JsProxy;

            // ***this line is ok***
            proxy.SmokeTest();
        }

    });        

    // ***this line fails (of course)***
    proxy.SmokeTest();

}

그러나 이로 인해 오류가 발생합니다. agApp.Content.JsProxy Silverlight onLoad 이벤트가 시작될 때까지 완전히 사용할 수 없으므로 JsProxy 필드를 사용할 수 없습니다.

JsProxy 클래스에 대한 액세스를 활성화하려면 어떻게 해야 합니까? 즉시 Silverlight 인스턴스를 만들 때 어떻게 될까요?while(_mutex);아마도 나쁜 생각 일 것입니다.

Silverlight 앱 인스턴스 생성 시 또 다른 추상화 계층이 구축되어 해당 함수가 한 번에 모든 SL 콘텐츠를 동기식으로 로드해야 하기 때문에 이렇게 해야 했습니다.

도움이 되었습니까?

해결책

이는 Silverlight와 JavaScript가 별도의 스레드에서 작동하기 때문입니다.해당 Silverlight 컨트롤을 로드하도록 브라우저에 요청한 경우에도 Silverlight는 로드가 완료될 때까지 기다리지 않고 다음 줄로 진행합니다.

Silverlight가 JS 프록시를 인스턴스화한 후에만 JS 프록시에 액세스할 수 있으므로 OnLoad 이벤트가 실행될 때까지 기다리거나(그러나 전체 Silverlight.xap이 완전히 로드된 후에만 실행됨) RegisterScriptableObject가 onYourJSProxyNameLoaded라는 JavaScript 메서드를 실행한 후에만 액세스할 수 있습니다. () 그러면 원하는 워크플로우에 맞게 다시 인라인으로 돌아갈 수 있습니다.

HTH.

-Scott Barnes / Rich Platforms 제품 관리자 / Microsoft.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top