Remove DispatcherTimerExtensions

This commit is contained in:
ClemensFischer 2025-10-25 20:29:25 +02:00
parent 96d4af1881
commit 2f6f7ee4eb
3 changed files with 29 additions and 53 deletions

View file

@ -1,46 +0,0 @@
#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;
#elif AVALONIA
using DependencyObject = Avalonia.AvaloniaObject;
using Avalonia.Threading;
#endif
using System;
namespace MapControl
{
internal static class DispatcherTimerExtensions
{
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();
}
}
}
}

View file

@ -62,7 +62,7 @@ namespace MapControl
loadingProgress = new Progress<double>(p => SetValue(LoadingProgressProperty, p));
updateTimer = this.CreateTimer(UpdateInterval);
updateTimer = new DispatcherTimer { Interval = UpdateInterval };
updateTimer.Tick += async (s, e) => await UpdateImageAsync();
}
@ -164,7 +164,15 @@ namespace MapControl
}
else
{
updateTimer.Run(!UpdateWhileViewportChanging);
if (!UpdateWhileViewportChanging)
{
updateTimer.Stop();
}
if (!updateTimer.IsEnabled)
{
updateTimer.Start();
}
}
}
@ -174,9 +182,12 @@ namespace MapControl
{
if (updateInProgress)
{
// Update image on next tick, start timer if not running.
// Update image on next timer tick.
//
updateTimer.Run();
if (!updateTimer.IsEnabled)
{
updateTimer.Start();
}
}
else
{

View file

@ -68,7 +68,7 @@ namespace MapControl
loadingProgress = new Progress<double>(p => SetValue(LoadingProgressProperty, p));
updateTimer = this.CreateTimer(UpdateInterval);
updateTimer = new DispatcherTimer { Interval = UpdateInterval };
updateTimer.Tick += (s, e) => Update(false);
MapPanel.SetRenderTransform(this, new MatrixTransform());
@ -185,7 +185,10 @@ namespace MapControl
parentMap.ViewportChanged += OnViewportChanged;
}
updateTimer.Run();
if (!updateTimer.IsEnabled)
{
updateTimer.Start();
}
}
}
@ -229,7 +232,15 @@ namespace MapControl
{
SetRenderTransform();
updateTimer.Run(!UpdateWhileViewportChanging);
if (!UpdateWhileViewportChanging)
{
updateTimer.Stop();
}
if (!updateTimer.IsEnabled)
{
updateTimer.Start();
}
}
}
}