2012-10-30 70 views
1

我正在嘗試執行以下操作。如何從ScriptSharp訪問JavaScript?

var handler = e => { handle(); item.Unbind("event", this); } 
item.Bind("event", handler); 

在JavaScript中,這將正常工作,但ScriptSharp取代JavaScript的這個參考與該代碼包含類方法的實例。我如何避免這種行爲,並從lambda本身獲得對lambda的引用?

回答

0

這裏是你如何能做到這一點(假設綁定需要委託與行動簽名):

SomeObject item = ...; 
Action handler = null; 

handler = delegate() { 
    // do something ... eg. call Handle(); 
    item.Unbind("event", handler); 
}; 
item.Bind("event", handler); 

而且,看這個問題:How to write a function in script# to be called with any object as this, not just with the instance of the class in which it is defined?一種技術用於編寫生成「這個」碼參考腳本。