A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

39 lines
1.1 KiB

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Rendering;
using CoreAnimation;
using Foundation;
using UIKit;
namespace Avalonia.iOS
{
class DisplayLinkTimer : IRenderTimer
{
public event Action<TimeSpan> Tick;
private Stopwatch _st = Stopwatch.StartNew();
public DisplayLinkTimer()
{
var link = CADisplayLink.Create(OnLinkTick);
TimerThread = new Thread(() =>
{
link.AddToRunLoop(NSRunLoop.Current, NSRunLoopMode.Common);
NSRunLoop.Current.Run();
});
TimerThread.Start();
UIApplication.Notifications.ObserveDidEnterBackground((_,__) => link.Paused = true);
UIApplication.Notifications.ObserveWillEnterForeground((_, __) => link.Paused = false);
}
public Thread TimerThread { get; }
public bool RunsInBackground => true;
private void OnLinkTick()
{
Tick?.Invoke(_st.Elapsed);
}
}
}