I have a strange issue. I want to Create a ContentView with a Mapsui MapView in it. But when I do this, the MapView is displayed but it is blank.
This is obviously wrong.
Here is a simple version of the issue:
MapView.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mapsui="clr-namespace:Mapsui.UI.Maui;assembly=Mapsui.UI.Maui"
x:Class="MauiApp7.Pages.MapView">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Text="Get Location" Grid.Row="0" Clicked="GetLocationAsync" ></Button>
<mapsui:MapView x:Name="_mapView"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
BackgroundColor="Gray"
Grid.Row="1"/>
</Grid>
</ContentView>
MapView.xaml.cs
using Mapsui.UI.Maui;
using MauiApp7.Pages.ViewModels;
namespace MauiApp7.Pages;
public partial class MapView : ContentView
{
public MapViewModel ViewModel => BindingContext as MapViewModel;
public MapView()
{
BindingContext = ServiceProvider.GetService<MapViewModel>();
_mapView = new Mapsui.UI.Maui.MapView();
_mapView.Map.Layers.Add(Mapsui.Tiling.OpenStreetMap.CreateTileLayer());
InitializeComponent();
}
private async void GetLocationAsync(object sender, EventArgs EventArgs)
{
//Do something
}
}
Then, to simplify things, I just set the content in MainPage to a map view: MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var map = new Pages.MapView();
Content = map;
}
}
I can't understand why this is.
If I set the MainPage.xaml.cs content directly to a
Mapsui.UI.Maui.MapView();
then that works. Like this:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var mapView = new Mapsui.UI.Maui.MapView();
mapView.Map?.Layers.Add(Mapsui.Tiling.OpenStreetMap.CreateTileLayer());
Content = mapView;
}
}
then I get my map correctly displayed.
Any thoughts on this?
Thanks!