question

frozen-3463 avatar image
0 Votes"
frozen-3463 asked LeonLu-MSFT answered

Xamarin.Form TouchEvent

I need implement a function on a Xamarin.Form.Android Application.as follows:
After five minutes of no operation,Page jump to LoginPage.
So is there a global touch Event in Xamarin.form?I could change it to implements

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

is there a global touch Event in Xamarin.form?

No, But Android have a DispatchTouchEvent in the MainActivity.cs.


We can write a SessionManager like this thread: https://stackoverflow.com/a/51727021/10627299


Then, Use it in the MainActivity.cs. When have a click, the task will run, after 30 seconds, HandleSessionExpired will be execute.


using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using Android.Views;

namespace DetectorNoAction.Droid
{
    [Activity(Label = "DetectorNoAction", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

         

            LoadApplication(new App());
        }

        public override void OnUserInteraction()
        {
            base.OnUserInteraction();

          
        }

         void HandleSessionExpired(object sender, EventArgs e)
        {
             App.Instance.DoLogoutAsync();
        }

        public  override bool DispatchTouchEvent(MotionEvent ev)
        {

            switch (ev.Action)
            {
                
                case MotionEventActions.Up:
                    SessionManager.Instance.SessionDuration = TimeSpan.FromSeconds(30);

                   
                    SessionManager.Instance.OnSessionExpired = HandleSessionExpired;
                    
                    SessionManager.Instance.StartTrackSessionAsync();
                    break;
                default:
                    SessionManager.Instance.EndTrackSession();
                    break;
            }
            return base.DispatchTouchEvent(ev);

        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}


After execute App.Instance.DoLogoutAsync();, it will open login page in the App.xaml.cs

   public partial class App : Application
   {
     public static App Instance { get; set; }

     public App()
     {
         InitializeComponent();
         Instance = this;

         MainPage = new MainPage();
     }


     public void DoLogoutAsync()
     {
         Console.WriteLine("========================");
         MainPage = new LoginPage();
     }


Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


[1]: https://stackoverflow.com/a/51727021/10627299

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.