How can I intercept a tapped/clicked event on application level in an Xamarin.Forms app?
I tried on the MainPage to use something like
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TappedCommand}" Tapped="TapGestureRecognizer_OnTapped"></TapGestureRecognizer>
</Grid.GestureRecognizers>
but it works only if I tap empty areas, touching a button doesn't fire the events.
Additionally I often navigate by changing the MainPage, so I would have to add such recognizer even if it worked.
I want to handle any touch/tap/click event in my app globally, is it possible in Xamarin.Forms?
3条答案
按热度按时间gxwragnw1#
This can be easily setup within the platform-specific apps and then use a
Xamarin.Forms
dependency service to subscribe/unsubscribe to the events.What you capture within those events is up to your needs, in this example I am just capturing and eventing the x/y values.
A generic EventArgs Class and DP Interface:
Add the following to the
MainActivity
of yourXamarin.Android
application:Android Dependency Implementation:
Usage in your Xamarin.Forms project:
Note: You should be assigning a delegate so can unsubscribe...
Output:
The same technique can be used for iOS, there are
TouchesBegan
,TouchesEnded
,TouchesMoved
andTouchesCancelled
that you can attach to depending upon your needs....06odsfpq2#
使用SushiHangover分辨率,对于iOS,它将是这样的:
cyej8jka3#
SushiHangover解决方案的iOS部分:
不要忘记在android和iOS部件的必要位置将x和y坐标除以DeviceDisplay. MainDisplayInfo. Density。