美好的一天。我在Embarcadero Xe8的C++ Builder中写作。我在 iOS 和 android 上做移动应用程序项目,遇到了这样的问题:我无法捕捉手机锁屏事件。我以前总是这样做:
bool TForm1::HandleApp(TApplicationEvent a, TObject *x)
{
if (a == TApplicationEvent::EnteredBackground)
{
MediaPlayer1->Stop();
}
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
_di_IFMXApplicationEventService a;
if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXApplicationEventService), &a))
{
a->SetApplicationEventHandler(TForm1::HandleApp);
}
}
但是一个错误:
\Unit1.cpp(33): 无法初始化类型为 'bool (closure *)(Fmx::P latform::TApplicationEvent, System::TObject __borland_class *__strong) __attribute((pcs("aapcs-vfp")))') 的参数,其左值类型为 'bool (__closure *)(Fmx::P latform::TApplicationEvent, System::TObject __borland_class *__strong)' 调频。Platform.hpp(252):将参数传递给参数 'AEventHandler' 这里
我不知道还能尝试做什么!你能帮帮我吗?
您的HandleApp()
方法缺少__fastcall
调用约定:
bool __fastcall TForm1::HandleApp(TApplicationEvent a, TObject *x)
此外,您对SetApplicationEventHandler()
的调用需要如下所示:
a->SetApplicationEventHandler(&HandleApp);
这很重要,因为事件处理程序是一个__closure
,所以它内部有两个指针 - 一个指向要调用的类方法的指针,以及一个指向调用该方法的对象实例的指针(方法的this
值)。仅按类名传递处理程序时,编译器不知道要对哪个对象实例执行操作,因此无法填充__closure
。 上面的语法允许编译器看到HandleApp
应该与Form1
对象相关联。