最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dependency injection - CustomPin Android Maui.NET - Stack Overflow

programmeradmin5浏览0评论

I'm having trouble retrieving a value when a PIN is selected in Android. I tried using dependency injection, but it didn't work. Is there another approach I could try? Also, could you share the code? I've already read all the documentation for dependency injection, but I still can't retrieve the value.

Shared Project interface

public delegate void PinRetrievedEventHandler(IMapPin pin);
    public class CustomPlatformService
    {
        public static event PinRetrievedEventHandler PinRetrieved;

        // Method to raise the PinRetrieved event
        public static void RaisePinRetrieved(IMapPin pin)
        {
            PinRetrieved?.Invoke(pin);
        }
    }

mMain Page ViewModel

private readonly CustomPlatformService _platformService;

constructor inject intermediate class

_platformService = DependencyService.Get<CustomPlatformService>();

CustomPlatformService.PinRetrieved += (pin) => HandlePinRetrieved(pin, null);

Declare on MauiProgram.CS

 builder.Services.AddSingleton<CustomPlatformService>();

 builder.ConfigureMauiHandlers(handlers =>
            {
                handlers.AddHandler<Microsoft.Maui.Controls.Maps.Map, CustomMapHandler>();
            });

This is the android class that I would like to return the pin id to the shared project on HandlePinRetrieved.

public class CustomMarkerClickListener(MapSettings mapHandler) : Java.Lang.Object, GoogleMap.IOnMarkerClickListener
    {
     
        int selected_location_id;
    
     
        public bool OnMarkerClick(Marker marker)
        {
            if (marker.Tag is Java.Lang.Integer tag)
            {
                int pinId = tag.IntValue();
                // Access the custom number (PinId) associated with the marker
                Console.WriteLine($"Marker clicked with PinId: {pinId}");
                selected_location_id = pinId;
                var pin = mapHandler.Markers.FirstOrDefault(x => x.marker.Id == marker.Id);
                pin.pin?.SendMarkerClick();
                marker.ShowInfoWindow();
                //marker.NotifyAll(); here I need to send that info as an alert form to the main shared project
     
            }
            return true; //
        }

I also tried this method, the only issue I have is that I have to pass the interface to the constructor and I cant do that because I will have to do several other changes.it has to be a easier way to do it.

发布评论

评论列表(0)

  1. 暂无评论