XAML-Map-Control/MapControl/Shared/DispatcherTimerExtensions.cs

47 lines
1 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
#if WPF
2024-05-22 21:20:49 +02:00
using System.Windows;
2024-05-22 11:25:32 +02:00
using System.Windows.Threading;
2024-05-20 23:24:34 +02:00
#elif UWP
2022-01-12 23:56:05 +01:00
using Windows.UI.Xaml;
2025-01-28 17:52:40 +01:00
#elif WINUI
global using DispatcherTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
using Microsoft.UI.Xaml;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using DependencyObject = Avalonia.AvaloniaObject;
using Avalonia.Threading;
2022-01-12 23:56:05 +01:00
#endif
2025-01-28 17:52:40 +01:00
using System;
2022-01-12 23:56:05 +01:00
namespace MapControl
{
2025-08-26 08:09:50 +02:00
internal static class DispatcherTimerExtensions
2022-01-12 23:56:05 +01:00
{
2025-01-28 17:52:40 +01:00
public static DispatcherTimer CreateTimer(this DependencyObject obj, TimeSpan interval)
2022-01-12 23:56:05 +01:00
{
2025-01-28 17:52:40 +01:00
#if WINUI
var timer = obj.DispatcherQueue.CreateTimer();
#else
var timer = new DispatcherTimer();
#endif
timer.Interval = interval;
return timer;
2022-01-12 23:56:05 +01:00
}
public static void Run(this DispatcherTimer timer, bool restart = false)
{
if (restart)
{
timer.Stop();
}
2025-01-28 17:52:40 +01:00
#if WINUI
if (!timer.IsRunning)
#else
2022-01-12 23:56:05 +01:00
if (!timer.IsEnabled)
2025-01-28 17:52:40 +01:00
#endif
2022-01-12 23:56:05 +01:00
{
timer.Start();
}
}
}
}