下面是用C#编写的使用SDK(进程内COM对象)的代码示例 我创建了一个类:TSwitcherMonitor,现在我要将回调函数分配给我的对象属性:OnSwitcherDisconnected属性。我在C#中有这个调用示例,该说明讨论了lambda表达式的双重使用。我已经在Google上搜索过,似乎Delphi没有lambda表达式。
这是C#中的调用: Q1:有一种方法可以使用invoke方法在Delphi中以相同的方式执行,或者我们必须以不同的方式执行此操作?
//Create callbacks object
m_switcherMonitor := TSwitcherMonitor.Create(Application.Handle);
// note: this invoke pattern ensures our callback is called in the main thread. We are making double
// use of lambda expressions here to achieve this.
// Essentially, the events will arrive at the callback class (implemented by our monitor classes)
// on a separate thread. We must marshell these to the main thread, and we're doing this by calling
// invoke on the Windows Forms object. The lambda expression is just a simplification./
m_switcherMonitor.OnSwitcherDisconnected += new SwitcherEventHandler((s, a) => this.Invoke((Action)(() => SwitcherDisconnected())));
第二季度:唯一认为我想要的是使用SDK进行回调。我的TSwitcherEventHandler声明是否正确?
在参考中,这是我的TSwitcherEventHandler声明和我的TSwitcherMonitor类:
Type
{TSwitcherEventHandler}
TSwitcherEventHandler = procedure(const sender: TObject; const args: TObject) of object;
{TSwitcherMonitor}
TSwitcherMonitor = Class(TComObject, IBMDSwitcherCallback)
private
FHwnd: HWND;
FSwitcherDisconnected: TSwitcherEventHandler;
published
constructor Create(hWnd: HWND);
public
function Notify(eventType: _BMDSwitcherEventType): HResult; stdcall;
property OnSwitcherDisconnected: TSwitcherEventHandler read FSwitcherDisconnected write FSwitcherDisconnected;
end;
implementation
{ TSwitcherMonitor }
constructor TSwitcherMonitor.Create(hWnd: HWND);
begin
FHwnd:= hWnd;
end;
function TSwitcherMonitor.Notify(eventType: _BMDSwitcherEventType): HRESULT; stdcall;
begin
if eventType = bmdSwitcherEventTypeDisconnected then
if assigned(FSwitcherDisconnected) then
FSwitcherDisconnected(self, nil);
result := S_OK;
end;
答案 0 :(得分:0)
我不是“Delphi专家”,但正如许多人所说的那样,德尔福摇滚!
C#行:
m_switcherMonitor.OnSwitcherDisconnected += new SwitcherEventHandler((s, a) => this.Invoke((Action)(() => SwitcherDisconnected())));
可以在Delphi中翻译为:
m_switcherMonitor.OnSwitcherDisconnected := switcherMonitor_OnSwitcherDisconnected;
现在我的示例应用程序工作了。 Delphi用指针做一些自动工作,当在Delphi中转换C#或C ++代码时,只需将其编写为更简单的表达式,Delphi就可以完成这项工作。一开始,当我们试图理解事物的运作方式时,使用这种黑魔法很奇怪也很难,但Delphi肯定是一个真正的RAD,因为他们做广告。