本文介绍了Xamarin.Forms 相当于覆盖 iOS 的 SendEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究不活动检测.

I'm working on inactivity detection.

我已经通过子类化 UIApplication 和覆盖 SendEvent 在 iOS 中成功地做到了这一点此处.

I have successfully done so in iOS by subclassing UIApplication and overriding SendEvent as outlined here.

我知道我可以分别为 iOS 和 Android 实现这一点,但我更愿意通过拦截所有触摸事件并重置我的计时器来使用跨平台表单方法.我也不想为我的所有页面添加触摸事件处理程序.

I know I could implement this separately for both iOS and Android, but I'd rather have a cross-platform Forms approach by intercepting all touch events and resetting my timers. I'd rather not have to add a touch event handler to all my pages either.

推荐答案

我找不到跨平台的方法.我能够通过将计时器相关信息留在核心 Forms 项目中并分别为 iOS 和 Android 实现触摸事件处理程序来实现这一点.我按照 OP 中的链接所述处理了 iOS 触摸事件,但对于 Android,由于 OnUserInteraction() 方法的存在,我采用了子类化 Activity 的方法.

I was unable to find a cross platform approach. I was able to accomplish this by leaving the timer related information in core Forms project and implement the touch event handlers separately for iOS and Android. I handled the iOS touch events as outlined in the link in the OP, but for Android I took the approach of subclassing Activity due to the presence of the OnUserInteraction() method.

最初我认为我必须强制 Xamarin 对我在 Xamarin 中使用的所有页面使用我的子类 Activity,但我错了.在 Xamarin 论坛上的 AdamMeaney 能够帮助为 Android 方面的事情提供解决方案,关于对 Android 活动进行子类化.事实证明,Xamarin 只使用一个从 Xamarin.Forms.Platform.Android.FormsAppCompatActivity 继承的 Activity.我在 Droid 项目中使用了 Xamarin 提供的 MainAcitivty.从那里,覆盖 OnUserInteraction() 被证明是非常简单的:

Initially I thought I would have to force Xamarin to use my subclassed Activity for all pages that I use in Xamarin, but I was mistaken. AdamMeaney over on the Xamarin forums was able to help with providing a solution for the Android side of things with regards to subclassing an Android activity. As it turns out, Xamarin only uses one Activity which inherits from Xamarin.Forms.Platform.Android.FormsAppCompatActivity. I used the MainAcitivty provided by Xamarin in the Droid project. From there, overriding the OnUserInteraction() proved to be quite simple:

public override void OnUserInteraction()
{
    base.OnUserInteraction();
    //Do other stuff
}

这篇关于Xamarin.Forms 相当于覆盖 iOS 的 SendEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:09