2009-08-07 65 views
1

我正在構建一個小型的ActiveX控件。我明白,ActiveX不能直接調用JavaScript函數,但需要通過事件工作。因此,我創建了一個事件,從codeproject的代碼示例中複製而來。從activex調用javascript函數

這個事件似乎只適用於某些情況:當調用立即引發事件的javascript函數時,它會起作用。當我的javascript函數調用進行長時間的操作,只比引發該事件的ActiveX方法,這是行不通的,這裏是我的意思的例子:

ActiveX事件:

[Guid("68BD4E0D-D7BC-4cf6-BEB7-CAB950161E79")] 
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] 
public interface ControlEvents 
{ 
    //Add a DispIdAttribute to any members in the source interface to specify the COM DispId. 
    [DispId(0x60020001)] 
    void OnClose(string redirectUrl); //This method will be visible from JS 
} 

的調用示例的作品:

[ComVisible(true)] 


public void Close() 
    { 
     if (OnClose != null) 
     { 
      OnClose("my test"); //Calling event that will be catched in JS 
     } 
     else 
     { 
      MessageBox.Show("No Event Attached"); //If no events are attached send message. 
     } 
    } 

失敗調用的一個例子:

[ComVisible(true)] 
     public void Open() 
     { 
      try 
      { 
       Start(); 
      } 
      catch (Exception e) 
      { 
       throw e; 
      } 
     } 

使用函數開始調用非常冗長的方法,每隔幾分鐘就會引發一次事件並將信息發送回javascript。

我似乎無法理解爲什麼它不起作用。少了什麼東西?如果問題不清楚,我很抱歉,我得到的錯誤不清楚,所以只要問一問,我就會改進問題。

回答

1

我在代碼中看不到任何事件聲明。東西應該是

public delegate void EvenHanderDelegate(string redirectUrl);

公共事件EnvenHanderDelegate OnClose;