How to display openstreetmap map on c# wpf ? i get black view screen
in mainwindow.xaml.cs i added a toggle button event to switch from current mode view to the interactive opengooglestreet map view.
at the top i added:
using Microsoft.Web.WebView2.Core;
and then
private bool isMapMode = false;
and
private async void BtnToggleMap_Click(object sender, RoutedEventArgs e)
{
isMapMode = !isMapMode;
if (isMapMode)
{
// Switch to MAP mode
BtnToggleMap.Content = "๐ก ืืืืจ ืืชืฆืืืช ืจืืืจ";
MapContainer.Visibility = Visibility.Visible;
RadarContainer.Visibility = Visibility.Collapsed;
string projectRoot = IOPath.GetFullPath(IOPath.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\"));
string htmlPath = IOPath.Combine(projectRoot, "Assets", "Map", "map.html");
string mapFolder = IOPath.Combine(projectRoot, "Assets", "Map");
try
{
if (!File.Exists(htmlPath))
{
Log("โ map.html not found in Assets/Map folder!");
return;
}
// Initialize WebView2
await MapView.EnsureCoreWebView2Async(null);
// Enable access for local radar images
string radarFolder = IOPath.Combine(projectRoot, "Assets", "Radar_Images");
MapView.CoreWebView2.SetVirtualHostNameToFolderMapping(
"radar.local", radarFolder, CoreWebView2HostResourceAccessKind.Allow);
// Enable access for map folder (for leaflet.css and leaflet.js)
MapView.CoreWebView2.SetVirtualHostNameToFolderMapping(
"map.local", mapFolder, CoreWebView2HostResourceAccessKind.Allow);
// Read and modify HTML to use virtual host
string htmlContent = File.ReadAllText(htmlPath);
MapView.NavigateToString(htmlContent);
Log("๐ Loaded interactive map mode with local Leaflet files.");
// Wait for map to load
await Task.Delay(1500);
// Send radar frames to map
await SendRadarFramesToMap();
}
catch (Exception ex)
{
Log($"โ Failed to initialize map: {ex.Message}");
}
}
else
{
// Switch back to RADAR mode
BtnToggleMap.Content = "๐บ ืืฆื ืืคื ืืื ืืจืืงืืืืืช";
MapContainer.Visibility = Visibility.Collapsed;
RadarContainer.Visibility = Visibility.Visible;
Log("๐ก Returned to radar image mode.");
}
}
and
private async void MapView_NavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs e)
{
if (e.IsSuccess)
{
Log("โ
Map loaded successfully.");
// Send radar frames after map loads
await SendRadarFramesToMap();
}
else
{
Log($"โ Map failed to load: {e.WebErrorStatus}");
}
}
it's reaching to the line
Log("โ
Map loaded successfully.");
but what i see is black on the right side when running the application.
this is the map.html file content a bit longer.
ืืคืช ืจืืืจ
i don't get any errors just black screen on the left side.
