Adding MapControl in xaml results in a “Catastropic failure”

主宰稳场 提交于 2019-12-01 15:24:28

I've tested your example and indeed there is such a problem also on my Phone.

As I have checked it is possible to set Collapsed from code - so as a workaround:

<Grid>
   <Maps:MapControl Name="myMap" Visibility="Visible" />
</Grid>

In the code behind:

public MainPage()
{
    this.InitializeComponent();
    this.Loaded += (sender, e) => myMap.Visibility = Visibility.Collapsed;
}

I've come up with a workaround for this. Instead of using visibility you can use the maps height/width properties to hide/show the map. Set them to 0 when you want to hide it, and set it to the parents width/height when you want to show it. Here is a code sample:

<Page
    x:Class="WP81App.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WP81App"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
     xmlns:Maps="using:Windows.UI.Xaml.Controls.Maps" 
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Maps:MapControl Name="MyMap" Height="0" Width="0" />
        <Button Content="Show Map" Click="ShowMapBtn_Clicked" HorizontalAlignment="Center"/>
    </Grid>
</Page>

Button Handler:

private void ShowMapBtn_Clicked(object sender, RoutedEventArgs e)
{
    var mapContainer = MyMap.Parent as FrameworkElement;
    MyMap.Width = mapContainer.ActualWidth;
    MyMap.Height = mapContainer.ActualHeight;

    //Hide the button
    (sender as Button).Visibility = Visibility.Collapsed;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!