MapMenuItem

This commit is contained in:
ClemensFischer 2025-09-20 14:02:42 +02:00
parent 616b2bf3f7
commit 3f16d1d637
10 changed files with 54 additions and 82 deletions

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net9.0-windows;net462</TargetFrameworks> <TargetFrameworks>net9.0-windows;net462</TargetFrameworks>
<LangVersion Condition="'$(TargetFramework)'=='net462'">8.0</LangVersion> <LangVersion Condition="'$(TargetFramework)'=='net462'">12.0</LangVersion>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<DefineConstants>WPF</DefineConstants> <DefineConstants>WPF</DefineConstants>
<RootNamespace>MapControl.MBTiles</RootNamespace> <RootNamespace>MapControl.MBTiles</RootNamespace>

View file

@ -4,16 +4,11 @@ using Avalonia.Media;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace MapControl.UiTools namespace MapControl.UiTools
{ {
public abstract partial class MapMenuItem : MenuItem public abstract partial class MapMenuItem : MenuItem
{ {
public abstract bool GetIsChecked(MapBase map);
public abstract Task ExecuteAsync(MapBase map);
protected MapMenuItem() protected MapMenuItem()
{ {
Icon = new TextBlock Icon = new TextBlock
@ -23,26 +18,8 @@ namespace MapControl.UiTools
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center, VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center,
}; };
Loaded += (s, e) => Loaded += (s, e) => Initialize();
{ Click += (s, e) => Execute();
if (DataContext is MapBase map)
{
IsChecked = GetIsChecked(map);
}
};
Click += async (s, e) =>
{
if (DataContext is MapBase map)
{
await ExecuteAsync(map);
foreach (var item in ParentMenuItems)
{
item.IsChecked = item.GetIsChecked(map);
}
}
};
} }
public string Text public string Text

View file

@ -29,7 +29,7 @@ namespace MapControl.UiTools
#endif #endif
public FrameworkElement MapLayer { get; set; } public FrameworkElement MapLayer { get; set; }
public override bool GetIsChecked(MapBase map) protected override bool GetIsChecked(MapBase map)
{ {
return MapLayer != null && map.Children.Contains(MapLayer); return MapLayer != null && map.Children.Contains(MapLayer);
} }

View file

@ -0,0 +1,30 @@
using System.Threading.Tasks;
namespace MapControl.UiTools
{
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()
{
if (DataContext is MapBase map)
{
IsEnabled = GetIsEnabled(map);
IsChecked = GetIsChecked(map);
}
}
private async void Execute()
{
if (DataContext is MapBase map)
{
await ExecuteAsync(map);
}
}
}
}

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
#if WPF #if WPF
using System.Windows.Markup; using System.Windows.Markup;
@ -14,20 +15,25 @@ using Avalonia.Metadata;
namespace MapControl.UiTools namespace MapControl.UiTools
{ {
#if WPF #if WPF
[ContentProperty(nameof(MapProjection))] [ContentProperty(nameof(CrsId))]
#elif UWP || WINUI #elif UWP || WINUI
[ContentProperty(Name = nameof(MapProjection))] [ContentProperty(Name = nameof(CrsId))]
#endif #endif
public partial class MapProjectionMenuItem : MapMenuItem public partial class MapProjectionMenuItem : MapMenuItem
{ {
#if AVALONIA #if AVALONIA
[Content] [Content]
#endif #endif
public string MapProjection { get; set; } public string CrsId { get; set; }
public override bool GetIsChecked(MapBase map) protected override bool GetIsEnabled(MapBase map)
{ {
return map.MapProjection.ToString() == MapProjection; return map.MapLayer is not IMapLayer mapLayer || mapLayer.SupportedCrsIds.Contains(CrsId);
}
protected override bool GetIsChecked(MapBase map)
{
return map.MapProjection.CrsId == CrsId;
} }
public override Task ExecuteAsync(MapBase map) public override Task ExecuteAsync(MapBase map)
@ -36,7 +42,7 @@ namespace MapControl.UiTools
{ {
try try
{ {
map.MapProjection = MapControl.MapProjection.Parse(MapProjection); map.MapProjection = MapProjection.Parse(CrsId);
} }
catch (Exception ex) catch (Exception ex)
{ {

View file

@ -6,6 +6,7 @@
<DefineConstants>UWP</DefineConstants> <DefineConstants>UWP</DefineConstants>
<RootNamespace>MapControl.UiTools</RootNamespace> <RootNamespace>MapControl.UiTools</RootNamespace>
<AssemblyTitle>XAML Map Control UI Tools Library for UWP</AssemblyTitle> <AssemblyTitle>XAML Map Control UI Tools Library for UWP</AssemblyTitle>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling> <DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
<DefaultLanguage>en-US</DefaultLanguage> <DefaultLanguage>en-US</DefaultLanguage>
</PropertyGroup> </PropertyGroup>

View file

@ -1,38 +1,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls; using System.Windows.Controls;
namespace MapControl.UiTools namespace MapControl.UiTools
{ {
public abstract partial class MapMenuItem : MenuItem public partial class MapMenuItem : MenuItem
{ {
public abstract bool GetIsChecked(MapBase map);
public abstract Task ExecuteAsync(MapBase map);
protected MapMenuItem() protected MapMenuItem()
{ {
Loaded += (s, e) => Loaded += (s, e) => Initialize();
{ Click += (s, e) => Execute();
if (DataContext is MapBase map)
{
IsChecked = GetIsChecked(map);
}
};
Click += async (s, e) =>
{
if (DataContext is MapBase map)
{
await ExecuteAsync(map);
foreach (var item in ParentMenuItems)
{
item.IsChecked = item.GetIsChecked(map);
}
}
};
} }
public string Text public string Text

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net9.0-windows;net462</TargetFrameworks> <TargetFrameworks>net9.0-windows;net462</TargetFrameworks>
<LangVersion Condition="'$(TargetFramework)'=='net462'">8.0</LangVersion> <LangVersion Condition="'$(TargetFramework)'=='net462'">12.0</LangVersion>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<DefineConstants>WPF</DefineConstants> <DefineConstants>WPF</DefineConstants>
<RootNamespace>MapControl.UiTools</RootNamespace> <RootNamespace>MapControl.UiTools</RootNamespace>

View file

@ -1,6 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
#if UWP #if UWP
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
@ -13,34 +12,15 @@ namespace MapControl.UiTools
{ {
public abstract partial class MapMenuItem : ToggleMenuFlyoutItem public abstract partial class MapMenuItem : ToggleMenuFlyoutItem
{ {
public abstract bool GetIsChecked(MapBase map);
public abstract Task ExecuteAsync(MapBase map);
protected MapMenuItem() protected MapMenuItem()
{ {
Loaded += (s, e) => Loaded += (s, e) =>
{ {
ParentMenuItems = ((Panel)VisualTreeHelper.GetParent(this)).Children.OfType<MapMenuItem>().ToList(); ParentMenuItems = ((Panel)VisualTreeHelper.GetParent(this)).Children.OfType<MapMenuItem>().ToList();
Initialize();
if (DataContext is MapBase map)
{
IsChecked = GetIsChecked(map);
}
}; };
Click += async (s, e) => Click += (s, e) => Execute();
{
if (DataContext is MapBase map)
{
await ExecuteAsync(map);
foreach (var item in ParentMenuItems)
{
item.IsChecked = item.GetIsChecked(map);
}
}
};
} }
protected IList<MapMenuItem> ParentMenuItems { get; private set; } protected IList<MapMenuItem> ParentMenuItems { get; private set; }

View file

@ -5,6 +5,7 @@
<DefineConstants>WINUI</DefineConstants> <DefineConstants>WINUI</DefineConstants>
<RootNamespace>MapControl.UiTools</RootNamespace> <RootNamespace>MapControl.UiTools</RootNamespace>
<AssemblyTitle>XAML Map Control UI Tools Library for WinUI</AssemblyTitle> <AssemblyTitle>XAML Map Control UI Tools Library for WinUI</AssemblyTitle>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>