我正在用.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");
}
}
}
有人可以帮助我吗?谢谢
答案 0 :(得分:0)
我刚刚解决了这个问题。
只需为ITestEvent.OnSetString添加一个“ DispId”即可。所以编码是:
[Guid("7279003B-0619-4E19-95BC-F22BD29CC950")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITestEvent
{
[DispId(0x0001)]
void OnSetString();
}