Xamarin.Forms等效于覆盖iOS的SendEvent [英] Xamarin.Forms equivalent to overriding iOS's SendEvent

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

问题描述

我正在研究不活动检测.

I'm working on inactivity detection.

我已经成功地在iOS中做到了这一点,方法是将UIApplication子类化并按

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

我知道我可以分别在iOS和Android上实现此功能,但是我希望通过拦截所有触摸事件并重置计时器来实现跨平台的Forms方法.我也不想在我的所有页面上添加一个触摸事件处理程序.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆