【问题标题】:Catch Lock Screen Event捕捉锁屏事件
【发布时间】:2016-04-22 08:40:43
【问题描述】:

美好的一天。我在 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 (closure *)(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”

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

【问题讨论】:

  • 附带说明,您不应该在 C++ 中使用 OnCreate 事件。它是一个 Delphi 习语,可以在 C++ 中产生非法行为。改写类构造函数 (__fastcall TForm1(TComponent*))。

标签: android c++builder firemonkey c++builder-xe8


【解决方案1】:

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

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

另外,您对SetApplicationEventHandler() 的调用需要改为:

a->SetApplicationEventHandler(&HandleApp);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    相关资源
    最近更新 更多