Simplified DispatcherTimer wrapper

This commit is contained in:
ClemensFischer 2025-01-28 17:52:40 +01:00
parent 4ec73292c3
commit d83493a498
7 changed files with 28 additions and 46 deletions

View file

@ -19,7 +19,6 @@ using Windows.UI.Xaml.Media;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using DispatcherTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
#endif
namespace MapControl

View file

@ -22,7 +22,6 @@ using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Media;
using DispatcherTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
#endif
namespace MapControl
@ -192,15 +191,15 @@ namespace MapControl
protected bool IsBaseMapLayer => parentMap != null && parentMap.Children.Count > 0 && parentMap.Children[0] == this;
protected abstract void SetRenderTransform();
protected abstract Task UpdateTileLayerAsync(bool tileSourceChanged);
protected Task LoadTilesAsync(IEnumerable<Tile> tiles, string cacheName)
{
return TileImageLoader.LoadTilesAsync(tiles, TileSource, cacheName, loadingProgress);
}
protected abstract void SetRenderTransform();
protected abstract Task UpdateTileLayerAsync(bool tileSourceChanged);
private Task UpdateTileLayer(bool tileSourceChanged)
{
updateTimer.Stop();

View file

@ -88,7 +88,10 @@ namespace MapControl
/// Loads a tile ImageSource asynchronously from an encoded frame buffer in a byte array.
/// This method is called by TileImageLoader when caching is enabled.
/// </summary>
public virtual Task<ImageSource> LoadImageAsync(byte[] buffer) => ImageLoader.LoadImageAsync(buffer);
public virtual Task<ImageSource> LoadImageAsync(byte[] buffer)
{
return ImageLoader.LoadImageAsync(buffer);
}
/// <summary>
/// Creates a TileSource instance from an Uri template string.

View file

@ -2,21 +2,30 @@
// Copyright © Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
#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 _, TimeSpan interval)
public static DispatcherTimer CreateTimer(this DependencyObject obj, TimeSpan interval)
{
return new DispatcherTimer { Interval = 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)
@ -25,8 +34,11 @@ namespace MapControl
{
timer.Stop();
}
#if WINUI
if (!timer.IsRunning)
#else
if (!timer.IsEnabled)
#endif
{
timer.Start();
}

View file

@ -17,7 +17,6 @@
</ItemGroup>
<ItemGroup>
<Compile Remove="..\Shared\Timer.cs" />
<Compile Remove="..\Shared\TypeConverters.cs" />
</ItemGroup>

View file

@ -32,7 +32,10 @@ namespace MapControl
public double Width { get; }
public double Height { get; }
public bool Contains(Point p) => p.X >= X && p.X <= X + Width && p.Y >= Y && p.Y <= Y + Height;
public bool Contains(Point p)
{
return p.X >= X && p.X <= X + Width && p.Y >= Y && p.Y <= Y + Height;
}
public static implicit operator Windows.Foundation.Rect(Rect r)
{

View file

@ -1,33 +0,0 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using System;
namespace MapControl
{
internal static class Timer
{
public static DispatcherQueueTimer CreateTimer(this DependencyObject obj, TimeSpan interval)
{
var timer = obj.DispatcherQueue.CreateTimer();
timer.Interval = interval;
return timer;
}
public static void Run(this DispatcherQueueTimer timer, bool restart = false)
{
if (restart)
{
timer.Stop();
}
if (!timer.IsRunning)
{
timer.Start();
}
}
}
}