我有一个包含三个实体的场景:
- 具有一个方法存根的接口
- 继承自"System::Windows::Forms::NativeWindow"并实现接口的类
- 一个包装类,具有类类型的私有成员和接口类型的公共属性。此类还有一个"KeyDown"事件成员,该成员将从窗口类调用/引发
这些是我正在使用的文件:
InAtiveWindow.h
#pragma once
public interface class INativeWindow
{
void Nothing();
};
CLINativeWindow.h
#pragma once
ref class NWHolder;
public ref class CLINativeWindow : System::Windows::Forms::NativeWindow, INativeWindow
{
public:
NWHolder^ Parent;
virtual void Nothing() sealed;
void DoIt();
};
临床窗口.cpp
#include "stdafx.h"
#include "CLINativeWindow.h"
void CLINativeWindow::Nothing()
{
Console::Write("None");
}
void CLINativeWindow::DoIt()
{
Parent->KeyDown(this, nullptr);
};
NWHolder.h
#pragma once
#include "INativeWindow.h"
#include "CLINativeWindow.h"
public ref class NWHolder
{
internal:
event System::Windows::Forms::KeyEventHandler^ KeyDown;
public:
virtual property INativeWindow^ OwnNativeWindow
{
INativeWindow^ __clrcall get() sealed;
void __clrcall set(INativeWindow^ value) sealed;
}
private:
CLINativeWindow^ nativeWindow_;
};
新华尔德.cpp
#include "stdafx.h"
#include "NWHolder.h"
INativeWindow^ NWHolder::OwnNativeWindow::get()
{
return nativeWindow_;
}
void NWHolder::OwnNativeWindow::set(INativeWindow^ value)
{
nativeWindow_ = dynamic_cast<CLINativeWindow^>(value);
}
在编译时,我收到此错误: Error 1 error C3767: 'NWHolder::KeyDown::raise': candidate function(s) not accessible ..NativeWindowsCLINativeWindow.cpp 10
有什么可以做的吗?我甚至尝试了#pragma make_public(System::Windows::Forms::KeyEventHandler)
但它失败了。
C++/CLI 事件的"raise"内部方法始终声明为受保护。在NWHolder上添加一个名为"FireKeyDownEvent"的方法,并赋予它任何你喜欢的可访问性。