mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Move Execute method to base class
This commit is contained in:
parent
a1b10b6e08
commit
45ab678d5b
|
|
@ -4,10 +4,11 @@ using Avalonia.Media;
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MapControl.UiTools
|
||||
{
|
||||
public class MapMenuItem : MenuItem
|
||||
public abstract class MapMenuItem : MenuItem
|
||||
{
|
||||
public MapMenuItem()
|
||||
{
|
||||
|
|
@ -25,7 +26,9 @@ namespace MapControl.UiTools
|
|||
set => Header = value;
|
||||
}
|
||||
|
||||
protected IEnumerable<MapMenuItem> ParentMenuItems => (Parent as ItemsControl)?.Items.OfType<MapMenuItem>();
|
||||
public abstract Task<bool> Execute(MapBase map);
|
||||
|
||||
protected IEnumerable<MapMenuItem> ParentMenuItems => ((ItemsControl)Parent).Items.OfType<MapMenuItem>();
|
||||
|
||||
protected override Type StyleKeyOverride => typeof(MenuItem);
|
||||
|
||||
|
|
|
|||
|
|
@ -35,10 +35,8 @@ namespace MapControl.UiTools
|
|||
{
|
||||
Click += async (s, e) =>
|
||||
{
|
||||
if (DataContext is MapBase map)
|
||||
if (DataContext is MapBase map && await Execute(map))
|
||||
{
|
||||
await Execute(map);
|
||||
|
||||
foreach (var item in ParentMenuItems.OfType<MapLayerMenuItem>())
|
||||
{
|
||||
item.IsChecked = map.Children.Contains(item.MapLayer);
|
||||
|
|
@ -47,19 +45,31 @@ namespace MapControl.UiTools
|
|||
};
|
||||
}
|
||||
|
||||
public virtual async Task Execute(MapBase map)
|
||||
public override async Task<bool> Execute(MapBase map)
|
||||
{
|
||||
map.MapLayer = MapLayer ?? (MapLayer = await MapLayerFactory.Invoke());
|
||||
IsChecked = true;
|
||||
var layer = MapLayer ?? (MapLayer = await MapLayerFactory.Invoke());
|
||||
|
||||
if (layer == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
map.MapLayer = layer;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class MapOverlayMenuItem : MapLayerMenuItem
|
||||
{
|
||||
public override async Task Execute(MapBase map)
|
||||
public override async Task<bool> Execute(MapBase map)
|
||||
{
|
||||
var layer = MapLayer ?? (MapLayer = await MapLayerFactory.Invoke());
|
||||
|
||||
if (layer == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (map.Children.Contains(layer))
|
||||
{
|
||||
map.Children.Remove(layer);
|
||||
|
|
@ -85,6 +95,8 @@ namespace MapControl.UiTools
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
#if WPF
|
||||
using System.Windows.Markup;
|
||||
#elif UWP
|
||||
|
|
@ -27,12 +28,10 @@ namespace MapControl.UiTools
|
|||
|
||||
public MapProjectionMenuItem()
|
||||
{
|
||||
Click += (s, e) =>
|
||||
Click += async (s, e) =>
|
||||
{
|
||||
if (DataContext is MapBase map)
|
||||
if (DataContext is MapBase map && await Execute(map))
|
||||
{
|
||||
Execute(map);
|
||||
|
||||
foreach (var item in ParentMenuItems.OfType<MapProjectionMenuItem>())
|
||||
{
|
||||
item.IsChecked = map.MapProjection.CrsId == item.MapProjection;
|
||||
|
|
@ -41,7 +40,7 @@ namespace MapControl.UiTools
|
|||
};
|
||||
}
|
||||
|
||||
public void Execute(MapBase map)
|
||||
public override Task<bool> Execute(MapBase map)
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
|
|
@ -58,7 +57,7 @@ namespace MapControl.UiTools
|
|||
}
|
||||
}
|
||||
|
||||
IsChecked = success;
|
||||
return Task.FromResult(success);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,16 +33,11 @@ namespace MapControl.UiTools
|
|||
{
|
||||
DataContext = Map;
|
||||
|
||||
if (Items.Count > 0)
|
||||
if (Items.Count > 0 &&
|
||||
Items[0] is MapMenuItem item &&
|
||||
await item.Execute(Map))
|
||||
{
|
||||
if (Items[0] is MapLayerMenuItem mapLayerItem)
|
||||
{
|
||||
await mapLayerItem.Execute(Map);
|
||||
}
|
||||
else if (Items[0] is MapProjectionMenuItem mapProjectionItem)
|
||||
{
|
||||
mapProjectionItem.Execute(Map);
|
||||
}
|
||||
item.IsChecked = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace MapControl.UiTools
|
||||
{
|
||||
public class MapMenuItem : MenuItem
|
||||
public abstract class MapMenuItem : MenuItem
|
||||
{
|
||||
public string Text
|
||||
{
|
||||
|
|
@ -12,7 +13,8 @@ namespace MapControl.UiTools
|
|||
set => Header = value;
|
||||
}
|
||||
|
||||
protected IEnumerable<MapMenuItem> ParentMenuItems
|
||||
=> (Parent as ItemsControl)?.Items.OfType<MapMenuItem>();
|
||||
public abstract Task<bool> Execute(MapBase map);
|
||||
|
||||
protected IEnumerable<MapMenuItem> ParentMenuItems => ((ItemsControl)Parent).Items.OfType<MapMenuItem>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
#if UWP
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Media;
|
||||
|
|
@ -10,9 +11,11 @@ using Microsoft.UI.Xaml.Media;
|
|||
|
||||
namespace MapControl.UiTools
|
||||
{
|
||||
public class MapMenuItem : ToggleMenuFlyoutItem
|
||||
public abstract class MapMenuItem : ToggleMenuFlyoutItem
|
||||
{
|
||||
public abstract Task<bool> Execute(MapBase map);
|
||||
|
||||
protected IEnumerable<MapMenuItem> ParentMenuItems
|
||||
=> (VisualTreeHelper.GetParent(this) as Panel)?.Children.OfType<MapMenuItem>();
|
||||
=> ((Panel)VisualTreeHelper.GetParent(this)).Children.OfType<MapMenuItem>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue