using Timer = System.Timers.Timer;
Run Code Online (Sandbox Code Playgroud)
应用程序.xaml.cs
Timer IdleTimer = new Timer( 60 * 1000); //each 1 minute
public App()
{
InitializeComponent();
MainPage = new AppShell();
IdleTimer.Elapsed += Idleimer_Elapsed;
IdleTimer.Start();
}
async void Idleimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine(":::Elapsed");
if (MainThread.IsMainThread)
await Shell.Current.Navigation.PopToRootAsync();
else
MainThread.BeginInvokeOnMainThread(async () => await Shell.Current.Navigation.PopToRootAsync());}
public void ResetIdleTimer()
{
IdleTimer.Stop();
IdleTimer.Start();
}
Run Code Online (Sandbox Code Playgroud)
要检测 Android 上的用户交互,您可以依赖OnUserInteraction()。
MainActivity.cs
public override void OnUserInteraction()
{
base.OnUserInteraction();
(App.Current as App).ResetIdleTimer();
}
Run Code Online (Sandbox Code Playgroud)
在 ios 上,您可以在应用程序级别源监听 touche 事件 。
程序.cs
static void Main(string[] args)
{
UIApplication.Main(args, typeof(CustomApplication), typeof(AppDelegate));
}
Run Code Online (Sandbox Code Playgroud)
自定义应用程序.cs
public class CustomApplication : UIApplication
{
public CustomApplication() : base()
{
}
public CustomApplication(IntPtr handle) : base(handle)
{
}
public CustomApplication(Foundation.NSObjectFlag t) : base(t)
{
}
public override void SendEvent(UIEvent uievent)
{
if (uievent.Type == UIEventType.Touches)
{
if (uievent.AllTouches.Cast<UITouch>().Any(t => t.Phase == UITouchPhase.Began))
{
(App.Current as App).ResetIdleTimer();
}
}
base.SendEvent(uievent);
}
Run Code Online (Sandbox Code Playgroud)
对于 Windows,您可以侦听一些本机窗口事件,例如WM_NCACTIVATE: 0x0086或WM_SETCURSOR 0x0020或WM_MOUSEACTIVATE。
我确信有一种更有效的方法来监听鼠标光标移动事件,由于某些原因在此级别没有报告(例如WM_MOUSEMOVE)。
MauiProgram.cs
.ConfigureLifecycleEvents(events =>
{
#if WINDOWS
events
.AddWindows(windows => windows
.OnPlatformMessage((window, args) =>
{
if (args.MessageId == Convert.ToUInt32("0x0086", 16) ||
args.MessageId == Convert.ToUInt32("0x0020", 16) ||
args.MessageId == Convert.ToUInt32("0x0021", 16) )
{
(App.Current as App).ResetIdleTimer();
}
}));
#endif
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
764 次 |
| 最近记录: |