【问题标题】:Can't fire an event from ActiveX to client with .Net(C#)无法使用 .Net(C#)从 ActiveX 向客户端触发事件
【发布时间】:2019-04-04 12:03:15
【问题描述】:

我正在用 .Net 编写一个 ActiveX 对象,并且需要该对象可以使用 ie11 向客户端页面触发事件,但是现在当我触发事件时,将抛出 NotImplementedException。

页面代码为:

<!DOCTYPE html>
<script language="javascript" for="RegisterTestClass" event="OnSetString()" type="text/javascript">
    alert("Event is raised");
</script>
<object id="RegisterTestClass" name="RegisterTestClass" style="width:0px;height:0px;display:none" classid="CLSID:336A359B-EEC4-47EB-AA8D-B9DD0D4D4B5B"></object>
<script type="text/javascript">
    function SetOCXString(varStr)
    {
        alert("SetOCXString");
        RegisterTestClass.SetString(varStr);
    }
</script>
<html>
<head>
    <meta charset="utf-8" />
    <title>Test OCX</title>
</head>
<body>
    <button onclick="SetOCXString('Hello World!');"> Click me!</button>
</body>
</html>

ActiveX 文件是:

[Guid("7279003B-0619-4E19-95BC-F22BD29CC950")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITestEvent
{
    void OnSetString();
}

[ComVisible(false)]
public delegate void EventHandlerTest();

[Guid("336A359B-EEC4-47EB-AA8D-B9DD0D4D4B5B")]
[ComSourceInterfaces(typeof(ITestEvent))]
public class RegisterTestClass
{
    public event EventHandlerTest OnSetString;

    public void SetString(string str)
    {
        MessageBox.Show("SetString:" + str);
        if (null != this.OnSetString)
        {
            try
            {
                MessageBox.Show("Event is not null:" + this.OnSetString.Target.ToString() + "," + this.OnSetString.Method.Name);
                this.OnSetString();
                MessageBox.Show("Event raised");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace, e.Source + e.Message);
            }
        }
        else
        {
            MessageBox.Show("Event is null");
        }
    }
}

谁能帮帮我?谢谢

【问题讨论】:

    标签: javascript c# activex ocx


    【解决方案1】:

    我刚刚解决了这个问题。

    只需为 ITestEvent.OnSetString 添加一个“DispId”。所以编码是:

    [Guid("7279003B-0619-4E19-95BC-F22BD29CC950")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ITestEvent
    {
        [DispId(0x0001)]
        void OnSetString();
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      相关资源
      最近更新 更多