Public Class MyIpyClass
Public Event MyEvent()
Public Sub New()
AddHandler MyEvent, HandleMyEvent()
End Sub
Private Function HandleMyEvent() as MyEventEventHandler
Console.WriteLine("Raised My Event")
Return Nothing
End Function
Public Sub RaiseMyEvent()
RaiseEvent MyEvent()
End Sub
End Class
IronPython代码:
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReferenceToFileAndPath("C:\\MyDllPath\\MyClassLibary.dll")
import MyClassLibary
from System.Windows.Forms import *
from System.Drawing import *
def mevent(*args): print("Event From IPY") # create IPython event hander for MyIpyClass.MyEvent
m = MyClassLibary.MyIpyClass() # create new instance of my class
m.MyEvent += mevent # Add IPythong Handler
m.RaiseMyEvent() # For Simplicity, Call MyIpyClass.MyEvent.RaiseEvent to raise the event
f = Form() # Create a Form to handle some events from System.Windows.Forms
f.Text = "My Form"
#defne a form click handler to print out the event args
def click(*args): print args
f.Click += click
b = Button() # Create a button and add it to the form
b.Text = "GO"
b.Name = "GO"
b.Location = Point(10,10)
#create a button click handler that prints hellow word
def click(f, a):
print("hello world")
#assign the button click handler
b.Click += click
#add the button to the form
f.Controls.Add(b)
#call Form.ShowDialog() - Show() blocks and will hang the form.
f.ShowDialog()
IronPython
扩展使用System.Reflection
。它自动导入可用的类型和方法。在您可以在DLL类库中公开您的类型。在
构建DLL后,可以在“IronPython”中导入它
还可以动态加载DLL和导入命名空间:
^{pr2}$在库中公开您的事件:
然后可以在“IronPython”中使用它们
要执行相反的操作并使用
IronPython
触发事件,只需在Vb.Net代码。在以下是工作代码:
在MyIpyClass.vb课堂文库
IronPython代码:
处理异常的一般方法是VB.net版程序应该对IronPython有一个引用/依赖关系。异常被抛出调用堆栈,直到它们被处理。您描述的体系结构表明它们是对等的,彼此之间没有直接的依赖关系,因此处理这些异常在
Catch
块中无法正常工作。在我认为这种方法有效的唯一方法是,如果您可以在IronPython中处理异常,方法是将异常发送到公共DLL中的一个方法,该方法将异常作为事件参数的一部分引发事件,就像您可以获得
UnhandledException
事件一样这将允许您的VB代码响应IronPython应用程序中发生的事情。在
相关问题 更多 >
编程相关推荐