XAML-Map-Control/MapControl/Shared/Timer.cs
2025-01-28 17:52:40 +01:00

48 lines
1.1 KiB
C#

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
#if WPF
using System.Windows;
using System.Windows.Threading;
#elif UWP
using Windows.UI.Xaml;
#elif WINUI
global using DispatcherTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
using Microsoft.UI.Xaml;
#endif
using System;
namespace MapControl
{
internal static class Timer
{
public static DispatcherTimer CreateTimer(this DependencyObject obj, TimeSpan interval)
{
#if WINUI
var timer = obj.DispatcherQueue.CreateTimer();
#else
var timer = new DispatcherTimer();
#endif
timer.Interval = interval;
return timer;
}
public static void Run(this DispatcherTimer timer, bool restart = false)
{
if (restart)
{
timer.Stop();
}
#if WINUI
if (!timer.IsRunning)
#else
if (!timer.IsEnabled)
#endif
{
timer.Start();
}
}
}
}