有趣的是堆栈给了我一个警告,说“具有匹配标题的问题因过于广泛而被关闭”,所以我会尽量保持重点。
问题是什么:
- 在我的地图控件上,我添加了一条折线(可能是 3 条,取决于服务器的响应)。
- 现在,当我添加
MapIcons
or anyMapElement
或 aMapItemsControl
(基本上是任何东西)时,它不会出现。调试后,我发现即使MyMap.MapElements.Add(myMapIcon);
运行完美,MyMap.MapElements.Count 只显示折线。仅添加多段线。 - 所以我删除了我的
Polyline
并猜测上面提到的所有组件都添加到MapControl
并且是可见的。 - 我挖得更深一点,发现每次我调用 my
MyMap.MapElements.Add(myMapIcon)
它都会调用添加已经添加的折线,因为折线是通过attached properties
和添加的binding
。
我的代码:
我的折线创建者附加课程:
public class PolylineCollection
{
public static readonly DependencyProperty PathDataCollectionProperty =
DependencyProperty.Register("PathDataCollection", typeof(ObservableCollection<IPolylinePath>), typeof(PolylineCollection), new PropertyMetadata(null, OnPathCollectionChanged));
public static void SetPathCollection(UIElement element, ObservableCollection<IPolylinePath> value)
{
element.SetValue(PathDataCollectionProperty, value);
}
public static ObservableCollection<IPolylinePath> GetPathCollection(UIElement element)
{
return (ObservableCollection<IPolylinePath>)element.GetValue(PathDataCollectionProperty);
}
private static void OnPathCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e?.NewValue != null)
{
if (e.NewValue is IEnumerable<IPolylinePath> polylineCollection)
{
var mapControl = d as MapControl;
if (mapControl == null)
{
throw new InvalidOperationException(
"Polyline.Track property can only be attached to a MapControl!");
}
mapControl.MapElements.Clear();
foreach (var polyline in GetPathCollection(mapControl))
{
mapControl.MapElements.Add(CreateMapPolyline(polyline));
}
}
}
}
private static MapPolyline CreateMapPolyline(IPolylinePath track)
{
var polyline = new MapPolyline()
{
StrokeColor = track.PolylineColor.Color,
StrokeThickness = track.PolylineThinkness,
StrokeDashed = false,
Path = track.PolylineGeopath,
};
if (track.PolylineColorMode == Enums.PolylineColorMode.Selected)
polyline.ZIndex = 5;
else
polyline.ZIndex = 1;
return polyline;
}
这PolylineColorMode
是一个快速enum
帮助我找出选择了哪条折线并执行操作的方法,请忽略它。
我的 XAML:
<maps:MapControl x:Name="MyMap" extentions:PolylineCollection.PathCollection="{x:Bind ViewModel.PolylinePoints,Mode=OneWay}">
其中 extensions 是上述类 ( polylineCollection
) 所在的命名空间。
代码隐藏:
MapIcon errorIcon = new MapIcon()
{
Location = ViewModel.CurrentDriftLocation,
NormalizedAnchorPoint = new Windows.Foundation.Point(0.5, 1.0),
Title = "Drifted",
ZIndex = 3,
Image = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new System.Uri("ms-appx:///SharedAssets/ErrorAnalysysAssets/DriftPoint.png"))
};
MyMap.MapElements.Add(errorIcon);
折线的 ViewModelBacking 属性
private ObservableCollection<Extentions.Map.Extentions.IPolylinePath> polylinePoints;
public ObservableCollection<Extentions.Map.Extentions.IPolylinePath> PolylinePoints
{
get { return polylinePoints; }
set { polylinePoints = value; RaisePropertyChanged(nameof(PolylinePoints)); }
}
IPolylinePath
public interface IPolylinePath
{
SolidColorBrush PolylineColor { get; }
int PolylineThinkness { get; set; }
string PolylineTag { get; set; }
IEnumerable<BasicGeoposition> PolylinePoints { get; set; }
Geopath PolylineGeopath { get; }
PolylineColorMode PolylineColorMode { get; set; }
}
谁能告诉我为什么会这样?或者我该如何处理它以便我的 mapControl 也可以显示其他元素?