c# - 将 map 控件的子级设置为最顶层

标签 c# uwp uwp-xaml

我正在向 map 控件添加一个堆栈面板。像下面这样

enter image description here

但是之前添加的一些点位于我的堆栈面板的顶部。如何将我的堆栈面板设置在最上面?

XAML:

<Grid x:Name="gridMain">
        <maps:MapControl
            x:Name="mapControl"
            ZoomInteractionMode="GestureAndControl"
            TiltInteractionMode="GestureAndControl"
            RotateInteractionMode="GestureAndControl">
            <!--ZoomLevel="{x:Bind ViewModel.ZoomLevel, Mode=OneWay}"
            Center="{x:Bind ViewModel.Center, Mode=OneWay}"-->


            <maps:MapItemsControl x:Name="MapItems">
                <maps:MapItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid Tapped="MagPoint_Tapped" maps:MapControl.NormalizedAnchorPoint="{Binding NormalizedAnchorPoint}" maps:MapControl.Location="{Binding Location}">
                            <Ellipse Canvas.ZIndex="0" Width="{Binding Mag5}" Height="{Binding Mag5}" Fill="{Binding MagColor}"/>
                            <!--<TextBlock Text="{Binding Mag}"/>-->
                        </Grid>

                    </DataTemplate>
                </maps:MapItemsControl.ItemTemplate>
            </maps:MapItemsControl>
        </maps:MapControl>
    </Grid>

并添加面板代码。

StackPanel sp = new StackPanel();
            sp.Background = new SolidColorBrush(Colors.White);
            sp.CornerRadius = new CornerRadius(15);
            sp.BorderBrush = new SolidColorBrush(Colors.LightGray);
            sp.BorderThickness = new Thickness(1);
            sp.Width = 260;
            sp.MinHeight = 180;
            sp.Padding = new Thickness(10);
            Canvas.SetZIndex(sp, 99999);

mapControl.Children.Add(sp);
            Windows.UI.Xaml.Controls.Maps.MapControl.SetLocation(sp, new Geopoint(new BasicGeoposition { Longitude = (double)fi.geometry.coordinates[0], Latitude = (double)fi.geometry.coordinates[1] }));
            Windows.UI.Xaml.Controls.Maps.MapControl.SetNormalizedAnchorPoint(sp, new Point(0.5, 1));

最佳答案

您设置 ZIndex 的方式不起作用,因为 StackPanel以及里面的元素MapItemsControl位于不同的主机中。

实时视觉树的帮助下,您可以了解它们的具体布局方式。

enter image description here

在上面的屏幕截图中,StackPanel的主机(即第一个 Canvas )位于 MapOverlayPresenter后面 s 主机(即插入 Canvas 的第二个 MapItemsControl)。所以为了有StackPanel坐在它们上面,您需要手动设置 ZIndex 第一个 Canvas1 .

一旦理解了这一点,解决方案就变得简单 -

Loaded += (s, e) =>
{
    // GetChildByName comes from
    // https://github.com/JustinXinLiu/Continuity/blob/0cc3d7556c747a060d40bae089b80eb845da84fa/Continuity/Extensions/UtilExtensions.cs#L44
    var layerGrid = mapControl.GetChildByName<Grid>("LayerGrid");
    var canvas1 = layerGrid.Children.First();

    Canvas.SetZIndex(canvas1, 1);
};

希望这有帮助!

关于c# - 将 map 控件的子级设置为最顶层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45782511/

相关文章:

javascript - 在具有有限字符的标签中显示数据

c# - Jwt 代码不适用于 .NET Core 2

c# - 在 UWP 应用程序中处理 Dispatcher 异常

c# - 直接在 XAML 中使用 .resw 文件中的字符串

c# - 在没有代理客户端的情况下使用 WCF 服务

c# - 如何从 TextBox 中的用户获取所需的键并在 Keyboard.isKeyDown(Key ."... ") 中使用它

c# - 带 WinUI 3 的托盘图标

c# - ListView 项 UWP 的替代颜色

converters - 如何在 Windows 10 通用应用程序中将 BitmapImage 转换为 WriteableBitmap?

c# - UWP 应用程序使用绑定(bind)显示/隐藏按钮