0

再会。我在 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):无法初始化类型为“TApplicationEventHandler”的参数(又名“bool(闭包 *)(Fmx::Platform::TApplicationEvent, System::TObject __borland_class *__strong)__attribute((pcs(“aapcs- vfp")))') 的左值类型为 'bool (__closure *)(Fmx::Platform::TApplicationEvent, System::TObject __borland_class *__strong)' FMX.Platform.hpp(252): 将参数传递给参数' AEventHandler'在这里

我不知道还能做什么!请你帮助我好吗?

4

1 回答 1

0

您的HandleApp()方法缺少__fastcall调用约定:

bool __fastcall  TForm1::HandleApp(TApplicationEvent a, TObject *x)

此外,您的呼叫SetApplicationEventHandler()需要是这样的:

a->SetApplicationEventHandler(&HandleApp);

这很重要,因为事件处理程序是 a __closure,因此它在其中携带两个指针 - 一个指向要调用的类方法的指针,以及一个指向调用该方法的对象实例的指针(方法的this值)。当您仅通过类名传递处理程序时,编译器不知道要作用于哪个对象实例,因此无法填充__closure. 上面的语法允许编译器看到HandleApp应该与Form1对象相关联。

于 2016-04-23T16:41:12.707 回答