【问题标题】:Change overlap order in a MapControl更改 MapControl 中的重叠顺序
【发布时间】:2016-09-28 11:13:31
【问题描述】:

我需要在 MapControl 中添加一些元素,例如:

<Maps:MapControl HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Loaded="MapControl_Loaded" x:Name="mymap">
    <Rectangle x:Name="r1" Width="100" Height="100" Fill="Red"/>
    <Rectangle x:Name="r2" Width="20" Height="20" Fill="Green"/>
    <Rectangle x:Name="r3" Width="20" Height="20" Fill="Blue"/>
    <Rectangle x:Name="r4" Width="20" Height="20" Fill="Yellow"/>
    <Rectangle x:Name="r5" Width="20" Height="20" Fill="Purple"/>
</Maps:MapControl>

我通过代码设置矩形的位置:

        MapControl.SetLocation(r1, new Geopoint(new BasicGeoposition() {
            Latitude = 45.6593049969524,
            Longitude = 8.97672694176435
        }));
        MapControl.SetNormalizedAnchorPoint(r1, new Point(0.5, 0.5));
        MapControl.SetLocation(r2, new Geopoint(new BasicGeoposition()
        {
            Latitude = 45.6592821981758,
            Longitude = 8.97627767175436
        }));
        MapControl.SetNormalizedAnchorPoint(r2, new Point(0.5, 0.5));
        MapControl.SetLocation(r3, new Geopoint(new BasicGeoposition()
        {
            Latitude = 45.6589662004262,
            Longitude = 8.97650314494967
        }));
        MapControl.SetNormalizedAnchorPoint(r3, new Point(0.5, 0.5));
        MapControl.SetLocation(r4, new Geopoint(new BasicGeoposition()
        {
            Latitude = 45.6604913715273,
            Longitude = 8.97657556459308
        }));
        MapControl.SetNormalizedAnchorPoint(r4, new Point(0.5, 0.5));
        MapControl.SetLocation(r5, new Geopoint(new BasicGeoposition()
        {
            Latitude = 45.6580915488303,
            Longitude = 8.97816779091954
        }));

矩形是从第一个 (r1) 到最后一个 (r5) 渲染的,因此 r5 与其他矩形重叠,r4 与 r3、r2、r1 等重叠...

现在我需要添加另一个矩形(r6),但我想在 r1 下添加 r6:

    Rectangle r6 = new Rectangle();
    r6.Width = 70;
    r6.Height = 70;
    r6.Fill = new SolidColorBrush(Colors.Coral);
    mymap.Children.Insert(0, r6);
    MapControl.SetLocation(r6, new Geopoint(new BasicGeoposition()
    {
        Latitude = 45.6594054121524,
        Longitude = 8.97751081734896
    }));
    MapControl.SetNormalizedAnchorPoint(r6, new Point(0.5, 0.5));

我使用“mymap.Children.Insert(0, r6);”而是“Children.Add”将 r6 放在第一个位置,但它不起作用,它被渲染为好像它是最后一个元素。

我尝试使用 Canvas.SetZIndex,但它不适用于 MapControl。

所以问题是,如何将 r6 添加到 MapControl 并将其呈现为第一个元素?

【问题讨论】:

  • 什么是根?是帆布吗?哦,它会是 Canvas.Zindex,而不是 .SetZindex
  • 根是 MapControl,不是 Canvas,所以 ZIndex 不起作用。在xaml中使用canvas.Zindex,在c#代码中必须使用Canvas.setZIndex(element,value)
  • @StefanoBalzarotti 它可能与this control一起工作得更好

标签: c# xaml uwp uwp-maps


【解决方案1】:

渲染顺序取决于控件添加到 MapControl 的时间。虽然您在第一个位置插入了 rectangle(r6) 但它是最后添加的,所以它首先被渲染。要更改 MapControl 中 XAML 控件的呈现顺序,我们需要删除 MapControl.Children 中的所有控件,然后以正确的顺序重新添加它们,如下所示:

private void myMap_Loaded(object sender, RoutedEventArgs e)
{
    //create a list to save the controls already in MapControl
    var originalChildren = new List<DependencyObject>();
    originalChildren = myMap.Children.ToList();

    //clear the controls
    myMap.Children.Clear();

    Rectangle r6 = new Rectangle();
    r6.Width = 70;
    r6.Height = 70;
    r6.Fill = new SolidColorBrush(Colors.Coral);

    //re-add into MapControl
    originalChildren.Insert(0, r6);

    foreach (var item in originalChildren)
    {
        myMap.Children.Add(item);
    }

    MapControl.SetLocation(r1, new Geopoint(new BasicGeoposition()
    {
        Latitude = 45.6593049969524,
        Longitude = 8.97672694176435
    }));
    MapControl.SetNormalizedAnchorPoint(r1, new Point(0.5, 0.5));
    MapControl.SetLocation(r2, new Geopoint(new BasicGeoposition()
    {
        Latitude = 45.6592821981758,
        Longitude = 8.97627767175436
    }));
    MapControl.SetNormalizedAnchorPoint(r2, new Point(0.5, 0.5));
    MapControl.SetLocation(r3, new Geopoint(new BasicGeoposition()
    {
        Latitude = 45.6589662004262,
        Longitude = 8.97650314494967
    }));
    MapControl.SetNormalizedAnchorPoint(r3, new Point(0.5, 0.5));
    MapControl.SetLocation(r4, new Geopoint(new BasicGeoposition()
    {
        Latitude = 45.6604913715273,
        Longitude = 8.97657556459308
    }));
    MapControl.SetNormalizedAnchorPoint(r4, new Point(0.5, 0.5));
    MapControl.SetLocation(r5, new Geopoint(new BasicGeoposition()
    {
        Latitude = 45.6580915488303,
        Longitude = 8.97816779091954
    }));

    MapControl.SetLocation(r6, new Geopoint(new BasicGeoposition()
    {
        Latitude = 45.6594054121524,
        Longitude = 8.97751081734896
    }));
    MapControl.SetNormalizedAnchorPoint(r6, new Point(0.5, 0.5));
}

【讨论】:

  • 我希望有一个更清洁的解决方案,但它确实有效。
猜你喜欢
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多