Xamarin.Form TouchEvent

Mettiriolo 21 Reputation points
2021-09-14T09:39:14.84+00:00

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,315 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,966 Reputation points Microsoft Vendor
    2021-09-15T04:15:18.397+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful