mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-05-07 13:37:47 +00:00
File scoped namespaces
This commit is contained in:
parent
c14377f976
commit
65aba44af6
152 changed files with 11962 additions and 12115 deletions
|
|
@ -22,109 +22,108 @@ using Avalonia.Media;
|
|||
using DependencyProperty = Avalonia.AvaloniaProperty;
|
||||
#endif
|
||||
|
||||
namespace MapControl.UiTools
|
||||
namespace MapControl.UiTools;
|
||||
|
||||
public static class HyperlinkText
|
||||
{
|
||||
public static class HyperlinkText
|
||||
private static readonly Regex regex = new Regex(@"\[([^\]]+)\]\(([^\)]+)\)");
|
||||
|
||||
/// <summary>
|
||||
/// Converts text containing hyperlinks in markdown syntax [text](url)
|
||||
/// to a collection of Run and Hyperlink inlines.
|
||||
/// </summary>
|
||||
public static IEnumerable<Inline> TextToInlines(this string text)
|
||||
{
|
||||
private static readonly Regex regex = new Regex(@"\[([^\]]+)\]\(([^\)]+)\)");
|
||||
var inlines = new List<Inline>();
|
||||
|
||||
/// <summary>
|
||||
/// Converts text containing hyperlinks in markdown syntax [text](url)
|
||||
/// to a collection of Run and Hyperlink inlines.
|
||||
/// </summary>
|
||||
public static IEnumerable<Inline> TextToInlines(this string text)
|
||||
while (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
var inlines = new List<Inline>();
|
||||
var match = regex.Match(text);
|
||||
|
||||
while (!string.IsNullOrEmpty(text))
|
||||
if (match.Success &&
|
||||
match.Groups.Count == 3 &&
|
||||
Uri.TryCreate(match.Groups[2].Value, UriKind.Absolute, out Uri uri))
|
||||
{
|
||||
var match = regex.Match(text);
|
||||
|
||||
if (match.Success &&
|
||||
match.Groups.Count == 3 &&
|
||||
Uri.TryCreate(match.Groups[2].Value, UriKind.Absolute, out Uri uri))
|
||||
inlines.Add(new Run
|
||||
{
|
||||
inlines.Add(new Run
|
||||
{
|
||||
Text = text.Substring(0, match.Index),
|
||||
Text = text.Substring(0, match.Index),
|
||||
#if WPF || AVALONIA
|
||||
BaselineAlignment = BaselineAlignment.Center
|
||||
BaselineAlignment = BaselineAlignment.Center
|
||||
#endif
|
||||
});
|
||||
|
||||
text = text.Substring(match.Index + match.Length);
|
||||
|
||||
var hyperlinkText = match.Groups[1].Value;
|
||||
#if AVALONIA
|
||||
var button = new HyperlinkButton
|
||||
{
|
||||
Content = hyperlinkText,
|
||||
NavigateUri = uri,
|
||||
Padding = new Thickness(0),
|
||||
Margin = new Thickness(0)
|
||||
};
|
||||
|
||||
var link = new InlineUIContainer
|
||||
{
|
||||
Child = button,
|
||||
BaselineAlignment = BaselineAlignment.Center
|
||||
};
|
||||
#else
|
||||
var run = new Run { Text = hyperlinkText };
|
||||
var link = new Hyperlink { NavigateUri = uri };
|
||||
link.Inlines.Add(run);
|
||||
#if WPF
|
||||
run.BaselineAlignment = BaselineAlignment.Center;
|
||||
link.BaselineAlignment = BaselineAlignment.Center;
|
||||
link.ToolTip = uri.ToString();
|
||||
link.RequestNavigate += (_, e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Process.Start(new ProcessStartInfo(e.Uri.ToString()) { UseShellExecute = true });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"{e.Uri}: {ex}");
|
||||
}
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
inlines.Add(link);
|
||||
}
|
||||
else
|
||||
{
|
||||
inlines.Add(new Run { Text = text });
|
||||
text = null;
|
||||
}
|
||||
}
|
||||
|
||||
return inlines;
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty InlinesSourceProperty =
|
||||
DependencyPropertyHelper.RegisterAttached<string>("InlinesSource", typeof(HyperlinkText), null,
|
||||
(element, oldValue, newValue) =>
|
||||
{
|
||||
if (element is TextBlock textBlock)
|
||||
{
|
||||
textBlock.Inlines.Clear();
|
||||
|
||||
foreach (var inline in TextToInlines(newValue))
|
||||
{
|
||||
textBlock.Inlines.Add(inline);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public static string GetInlinesSource(TextBlock element)
|
||||
{
|
||||
return (string)element.GetValue(InlinesSourceProperty);
|
||||
text = text.Substring(match.Index + match.Length);
|
||||
|
||||
var hyperlinkText = match.Groups[1].Value;
|
||||
#if AVALONIA
|
||||
var button = new HyperlinkButton
|
||||
{
|
||||
Content = hyperlinkText,
|
||||
NavigateUri = uri,
|
||||
Padding = new Thickness(0),
|
||||
Margin = new Thickness(0)
|
||||
};
|
||||
|
||||
var link = new InlineUIContainer
|
||||
{
|
||||
Child = button,
|
||||
BaselineAlignment = BaselineAlignment.Center
|
||||
};
|
||||
#else
|
||||
var run = new Run { Text = hyperlinkText };
|
||||
var link = new Hyperlink { NavigateUri = uri };
|
||||
link.Inlines.Add(run);
|
||||
#if WPF
|
||||
run.BaselineAlignment = BaselineAlignment.Center;
|
||||
link.BaselineAlignment = BaselineAlignment.Center;
|
||||
link.ToolTip = uri.ToString();
|
||||
link.RequestNavigate += (_, e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Process.Start(new ProcessStartInfo(e.Uri.ToString()) { UseShellExecute = true });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"{e.Uri}: {ex}");
|
||||
}
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
inlines.Add(link);
|
||||
}
|
||||
else
|
||||
{
|
||||
inlines.Add(new Run { Text = text });
|
||||
text = null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetInlinesSource(TextBlock element, string value)
|
||||
{
|
||||
element.SetValue(InlinesSourceProperty, value);
|
||||
}
|
||||
return inlines;
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty InlinesSourceProperty =
|
||||
DependencyPropertyHelper.RegisterAttached<string>("InlinesSource", typeof(HyperlinkText), null,
|
||||
(element, oldValue, newValue) =>
|
||||
{
|
||||
if (element is TextBlock textBlock)
|
||||
{
|
||||
textBlock.Inlines.Clear();
|
||||
|
||||
foreach (var inline in TextToInlines(newValue))
|
||||
{
|
||||
textBlock.Inlines.Add(inline);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public static string GetInlinesSource(TextBlock element)
|
||||
{
|
||||
return (string)element.GetValue(InlinesSourceProperty);
|
||||
}
|
||||
|
||||
public static void SetInlinesSource(TextBlock element, string value)
|
||||
{
|
||||
element.SetValue(InlinesSourceProperty, value);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,22 +13,21 @@ using DependencyProperty = Avalonia.AvaloniaProperty;
|
|||
using FrameworkElement = Avalonia.Controls.Control;
|
||||
#endif
|
||||
|
||||
namespace MapControl.UiTools
|
||||
namespace MapControl.UiTools;
|
||||
|
||||
public partial class MapLayerInfo : UserControl
|
||||
{
|
||||
public partial class MapLayerInfo : UserControl
|
||||
public static readonly DependencyProperty MapLayerProperty =
|
||||
DependencyPropertyHelper.Register<MapLayerInfo, FrameworkElement>(nameof(MapLayer), null);
|
||||
|
||||
public FrameworkElement MapLayer
|
||||
{
|
||||
public static readonly DependencyProperty MapLayerProperty =
|
||||
DependencyPropertyHelper.Register<MapLayerInfo, FrameworkElement>(nameof(MapLayer), null);
|
||||
get => (FrameworkElement)GetValue(MapLayerProperty);
|
||||
set => SetValue(MapLayerProperty, value);
|
||||
}
|
||||
|
||||
public FrameworkElement MapLayer
|
||||
{
|
||||
get => (FrameworkElement)GetValue(MapLayerProperty);
|
||||
set => SetValue(MapLayerProperty, value);
|
||||
}
|
||||
|
||||
public MapLayerInfo()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
public MapLayerInfo()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,88 +15,87 @@ using Avalonia.Metadata;
|
|||
using FrameworkElement = Avalonia.Controls.Control;
|
||||
#endif
|
||||
|
||||
namespace MapControl.UiTools
|
||||
{
|
||||
namespace MapControl.UiTools;
|
||||
|
||||
#if WPF
|
||||
[ContentProperty(nameof(MapLayer))]
|
||||
[ContentProperty(nameof(MapLayer))]
|
||||
#elif UWP || WINUI
|
||||
[ContentProperty(Name = nameof(MapLayer))]
|
||||
[ContentProperty(Name = nameof(MapLayer))]
|
||||
#endif
|
||||
public partial class MapLayerMenuItem : MapMenuItem
|
||||
{
|
||||
public partial class MapLayerMenuItem : MapMenuItem
|
||||
{
|
||||
#if AVALONIA
|
||||
[Content]
|
||||
[Content]
|
||||
#endif
|
||||
public FrameworkElement MapLayer { get; set; }
|
||||
public FrameworkElement MapLayer { get; set; }
|
||||
|
||||
protected override bool GetIsChecked(MapBase map)
|
||||
{
|
||||
return MapLayer != null && map.Children.Contains(MapLayer);
|
||||
}
|
||||
|
||||
public override Task ExecuteAsync(MapBase map)
|
||||
{
|
||||
if (MapLayer != null)
|
||||
{
|
||||
map.MapLayer = MapLayer;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
protected override bool GetIsChecked(MapBase map)
|
||||
{
|
||||
return MapLayer != null && map.Children.Contains(MapLayer);
|
||||
}
|
||||
|
||||
public partial class MapOverlayMenuItem : MapLayerMenuItem
|
||||
public override Task ExecuteAsync(MapBase map)
|
||||
{
|
||||
public string SourcePath { get; set; }
|
||||
|
||||
public int InsertOrder { get; set; }
|
||||
|
||||
public double OverlayOpacity { get; set; } = 1d;
|
||||
|
||||
public override async Task ExecuteAsync(MapBase map)
|
||||
if (MapLayer != null)
|
||||
{
|
||||
if (MapLayer == null)
|
||||
{
|
||||
await CreateMapLayer();
|
||||
}
|
||||
|
||||
if (MapLayer != null)
|
||||
{
|
||||
if (map.Children.Contains(MapLayer))
|
||||
{
|
||||
map.Children.Remove(MapLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
var insertIndex = ParentMenuItems
|
||||
.OfType<MapOverlayMenuItem>()
|
||||
.Where(item => item.InsertOrder <= InsertOrder && item.GetIsChecked(map))
|
||||
.Count();
|
||||
|
||||
if (map.MapLayer != null)
|
||||
{
|
||||
insertIndex++;
|
||||
}
|
||||
|
||||
map.Children.Insert(insertIndex, MapLayer);
|
||||
}
|
||||
}
|
||||
map.MapLayer = MapLayer;
|
||||
}
|
||||
|
||||
protected virtual async Task CreateMapLayer()
|
||||
{
|
||||
var ext = Path.GetExtension(SourcePath).ToLower();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
if (ext == ".kmz" || ext == ".kml")
|
||||
public partial class MapOverlayMenuItem : MapLayerMenuItem
|
||||
{
|
||||
public string SourcePath { get; set; }
|
||||
|
||||
public int InsertOrder { get; set; }
|
||||
|
||||
public double OverlayOpacity { get; set; } = 1d;
|
||||
|
||||
public override async Task ExecuteAsync(MapBase map)
|
||||
{
|
||||
if (MapLayer == null)
|
||||
{
|
||||
await CreateMapLayer();
|
||||
}
|
||||
|
||||
if (MapLayer != null)
|
||||
{
|
||||
if (map.Children.Contains(MapLayer))
|
||||
{
|
||||
MapLayer = await GroundOverlay.CreateAsync(SourcePath);
|
||||
map.Children.Remove(MapLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
MapLayer = await GeoImage.CreateAsync(SourcePath);
|
||||
}
|
||||
var insertIndex = ParentMenuItems
|
||||
.OfType<MapOverlayMenuItem>()
|
||||
.Where(item => item.InsertOrder <= InsertOrder && item.GetIsChecked(map))
|
||||
.Count();
|
||||
|
||||
MapLayer.Opacity = OverlayOpacity;
|
||||
if (map.MapLayer != null)
|
||||
{
|
||||
insertIndex++;
|
||||
}
|
||||
|
||||
map.Children.Insert(insertIndex, MapLayer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual async Task CreateMapLayer()
|
||||
{
|
||||
var ext = Path.GetExtension(SourcePath).ToLower();
|
||||
|
||||
if (ext == ".kmz" || ext == ".kml")
|
||||
{
|
||||
MapLayer = await GroundOverlay.CreateAsync(SourcePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
MapLayer = await GeoImage.CreateAsync(SourcePath);
|
||||
}
|
||||
|
||||
MapLayer.Opacity = OverlayOpacity;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,29 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace MapControl.UiTools
|
||||
namespace MapControl.UiTools;
|
||||
|
||||
public abstract partial class MapMenuItem
|
||||
{
|
||||
public abstract partial class MapMenuItem
|
||||
public abstract Task ExecuteAsync(MapBase map);
|
||||
|
||||
protected abstract bool GetIsChecked(MapBase map);
|
||||
|
||||
protected virtual bool GetIsEnabled(MapBase map) => true;
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
public abstract Task ExecuteAsync(MapBase map);
|
||||
|
||||
protected abstract bool GetIsChecked(MapBase map);
|
||||
|
||||
protected virtual bool GetIsEnabled(MapBase map) => true;
|
||||
|
||||
private void Initialize()
|
||||
if (DataContext is MapBase map)
|
||||
{
|
||||
if (DataContext is MapBase map)
|
||||
{
|
||||
IsEnabled = GetIsEnabled(map);
|
||||
IsChecked = GetIsChecked(map);
|
||||
}
|
||||
IsEnabled = GetIsEnabled(map);
|
||||
IsChecked = GetIsChecked(map);
|
||||
}
|
||||
}
|
||||
|
||||
private async void Execute()
|
||||
private async void Execute()
|
||||
{
|
||||
if (DataContext is MapBase map)
|
||||
{
|
||||
if (DataContext is MapBase map)
|
||||
{
|
||||
await ExecuteAsync(map);
|
||||
}
|
||||
await ExecuteAsync(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,53 +12,52 @@ using Microsoft.UI.Xaml.Markup;
|
|||
using Avalonia.Metadata;
|
||||
#endif
|
||||
|
||||
namespace MapControl.UiTools
|
||||
{
|
||||
namespace MapControl.UiTools;
|
||||
|
||||
#if WPF
|
||||
[ContentProperty(nameof(CrsId))]
|
||||
[ContentProperty(nameof(CrsId))]
|
||||
#elif UWP || WINUI
|
||||
[ContentProperty(Name = nameof(CrsId))]
|
||||
[ContentProperty(Name = nameof(CrsId))]
|
||||
#endif
|
||||
public partial class MapProjectionMenuItem : MapMenuItem
|
||||
{
|
||||
public partial class MapProjectionMenuItem : MapMenuItem
|
||||
{
|
||||
#if AVALONIA
|
||||
[Content]
|
||||
[Content]
|
||||
#endif
|
||||
public string CrsId { get; set; }
|
||||
public string CrsId { get; set; }
|
||||
|
||||
protected override bool GetIsEnabled(MapBase map)
|
||||
protected override bool GetIsEnabled(MapBase map)
|
||||
{
|
||||
var supportedCrsIds = map.MapLayer switch
|
||||
{
|
||||
var supportedCrsIds = map.MapLayer switch
|
||||
WmsImageLayer wmsImageLayer => wmsImageLayer.SupportedCrsIds,
|
||||
WmtsTileLayer wmtsTileLayer => wmtsTileLayer.TileMatrixSets.Keys,
|
||||
MapTileLayer => [WebMercatorProjection.DefaultCrsId],
|
||||
_ => null
|
||||
};
|
||||
|
||||
return supportedCrsIds == null || supportedCrsIds.Contains(CrsId);
|
||||
}
|
||||
|
||||
protected override bool GetIsChecked(MapBase map)
|
||||
{
|
||||
return map.MapProjection.CrsId == CrsId;
|
||||
}
|
||||
|
||||
public override Task ExecuteAsync(MapBase map)
|
||||
{
|
||||
if (!GetIsChecked(map))
|
||||
{
|
||||
try
|
||||
{
|
||||
WmsImageLayer wmsImageLayer => wmsImageLayer.SupportedCrsIds,
|
||||
WmtsTileLayer wmtsTileLayer => wmtsTileLayer.TileMatrixSets.Keys,
|
||||
MapTileLayer => [WebMercatorProjection.DefaultCrsId],
|
||||
_ => null
|
||||
};
|
||||
|
||||
return supportedCrsIds == null || supportedCrsIds.Contains(CrsId);
|
||||
}
|
||||
|
||||
protected override bool GetIsChecked(MapBase map)
|
||||
{
|
||||
return map.MapProjection.CrsId == CrsId;
|
||||
}
|
||||
|
||||
public override Task ExecuteAsync(MapBase map)
|
||||
{
|
||||
if (!GetIsChecked(map))
|
||||
{
|
||||
try
|
||||
{
|
||||
map.MapProjection = MapProjection.Parse(CrsId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"MapProjection.Parse: {ex.Message}");
|
||||
}
|
||||
map.MapProjection = MapProjection.Parse(CrsId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"MapProjection.Parse: {ex.Message}");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,36 +14,35 @@ using Avalonia.Controls;
|
|||
using DependencyProperty = Avalonia.AvaloniaProperty;
|
||||
#endif
|
||||
|
||||
namespace MapControl.UiTools
|
||||
namespace MapControl.UiTools;
|
||||
|
||||
public partial class MenuButton : Button
|
||||
{
|
||||
public partial class MenuButton : Button
|
||||
public static readonly DependencyProperty MapProperty =
|
||||
DependencyPropertyHelper.Register<MenuButton, MapBase>(nameof(Map), null,
|
||||
async (button, oldValue, newValue) => await button.Initialize());
|
||||
|
||||
public MapBase Map
|
||||
{
|
||||
public static readonly DependencyProperty MapProperty =
|
||||
DependencyPropertyHelper.Register<MenuButton, MapBase>(nameof(Map), null,
|
||||
async (button, oldValue, newValue) => await button.Initialize());
|
||||
get => (MapBase)GetValue(MapProperty);
|
||||
set => SetValue(MapProperty, value);
|
||||
}
|
||||
|
||||
public MapBase Map
|
||||
private async Task Initialize()
|
||||
{
|
||||
if (Map != null)
|
||||
{
|
||||
get => (MapBase)GetValue(MapProperty);
|
||||
set => SetValue(MapProperty, value);
|
||||
}
|
||||
DataContext = Map;
|
||||
|
||||
private async Task Initialize()
|
||||
{
|
||||
if (Map != null)
|
||||
var initialItem =
|
||||
Items.OfType<MapMenuItem>().FirstOrDefault(item => item.IsChecked) ??
|
||||
Items.OfType<MapMenuItem>().FirstOrDefault();
|
||||
|
||||
if (initialItem != null)
|
||||
{
|
||||
DataContext = Map;
|
||||
initialItem.IsChecked = true;
|
||||
|
||||
var initialItem =
|
||||
Items.OfType<MapMenuItem>().FirstOrDefault(item => item.IsChecked) ??
|
||||
Items.OfType<MapMenuItem>().FirstOrDefault();
|
||||
|
||||
if (initialItem != null)
|
||||
{
|
||||
initialItem.IsChecked = true;
|
||||
|
||||
await initialItem.ExecuteAsync(Map);
|
||||
}
|
||||
await initialItem.ExecuteAsync(Map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,33 +13,32 @@ using Microsoft.UI.Xaml.Data;
|
|||
using Avalonia.Data.Converters;
|
||||
#endif
|
||||
|
||||
namespace MapControl.UiTools
|
||||
namespace MapControl.UiTools;
|
||||
|
||||
internal partial class ProgressVisibilityConverter : IValueConverter
|
||||
{
|
||||
internal partial class ProgressVisibilityConverter : IValueConverter
|
||||
public object Convert(object value, Type targetType, object parameter, string language)
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, string language)
|
||||
{
|
||||
var visible = (double)value < 1d;
|
||||
var visible = (double)value < 1d;
|
||||
#if AVALONIA
|
||||
return visible;
|
||||
return visible;
|
||||
#else
|
||||
return visible ? Visibility.Visible : Visibility.Collapsed;
|
||||
return visible ? Visibility.Visible : Visibility.Collapsed;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return Convert(value, targetType, parameter, culture.ToString());
|
||||
}
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return Convert(value, targetType, parameter, culture.ToString());
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, string language)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public object ConvertBack(object value, Type targetType, object parameter, string language)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue