我正在創建一個Silverlight應用程序,該應用程序將嚴重依賴javascripted。Javascript/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());
}
然而,由於這是一個Javascript - 重型應用程序,它必須通過javascript本身加載。
I.e.東西alongs:
// 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字段不可引發錯誤。
當我創建Silverlight實例時,如何才能立即訪問JsProxy類? (_mutex);可能是一個壞主意。
我不得不這樣做,因爲在創建Silverlight應用程序實例時會有另一層抽象,因此函數必須一次同步加載所有SL內容。