fis-sst/BlazorMaps

NullReferenceException while assign events in OnAfterRenderAsync()

Knutselsmurf1 opened this issue · 2 comments

Describe the bug
If I assign an event in OnAfterRenderAsync() a NullReferenceException is given.

To Reproduce
my .razor file:

@page "/"

<Map @ref="mapRef" MapOptions="@mapOptions"></Map>

<style>
  #mapId {
    height: 400px;
  }
</style>

my .cs file:

using System.Diagnostics;
using FisSst.BlazorMaps;

namespace TestMap.Pages
{
  public sealed partial class Index
  {
    private Map? mapRef;

    private readonly MapOptions mapOptions = new()
    {
      DivId = "mapId",
      Center = new LatLng(50.0, 18.0),
      Zoom = 13,
      UrlTileLayer = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
      SubOptions = new MapSubOptions()
      {
        Attribution = "&copy; <a lhref='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>",
        TileSize = 512,
        ZoomOffset = -1,
        MaxZoom = 19,
      }
    };

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
      if (firstRender)
      {
        // this is a workaround to ensure other OnAfterRenderAsync are called first so
        // the following call can be called without errors: await mapRef.OnMouseDown(MapMouseDown);
        for (int i = 0; i < 20; i++)
        {
          //await Task.Delay(1);
        }

        if (mapRef != null)
        {
            await mapRef.OnClick(MapMouseDown);
        }
      }
    }
    
    private async Task MapMouseDown(MouseEvent e)
    {
      Debug.WriteLine("MapMouseDown");
    }
  }
}

Expected behavior
I want to be able to assign events in OnAfterRenderAsync() without workarounds like the await Task.Delay(1);

Screenshots
image