After binding using Objective sharpie objective C protocol delegate methods are not getting invoked

I am planning to bind IndoorAtlas iOS native sdk with Xamarin.ios.
I have generated API Definition and Structs file successfully. But Issue is while Implementing Protocols methods. As those are not getting invoked.
So do we need to handle it in special way?
Below is the code from API definition and Implementation file.
Method-DidUpdateLocations ,DidEnterRegion.

API Definition File
// @protocol IALocationManagerDelegate
[Protocol, Model]
[BaseType(typeof(NSObject))]
interface IALocationManagerDelegate
{
// @optional -(void)indoorLocationManager:(IALocationManager * _Nonnull)manager didUpdateLocations:(NSArray * _Nonnull)locations;
[Export("indoorLocationManager:didUpdateLocations:")]
//[Verify(StronglyTypedNSArray)]
void DidUpdateLocations(IALocationManager manager, IALocation[] locations);
// @optional -(void)indoorLocationManager:(IALocationManager * _Nonnull)manager didEnterRegion:(IARegion * _Nonnull)region;
[Export("indoorLocationManager:didEnterRegion:")]
void DidEnterRegion(IALocationManager manager, IARegion region);
}
// @interface IALocationManager : NSObject
[BaseType(typeof(NSObject))]
interface IALocationManager
{

[Wrap("WeakDelegate")]
[NullAllowed]
IALocationManagerDelegate Delegate { get; set; }

// @property (readwrite, nonatomic, weak) id<IALocationManagerDelegate> _Nullable delegate;
[NullAllowed, Export("delegate", ArgumentSemantic.Weak)]
NSObject WeakDelegate { get; set; }

}
ViewController File
[Export("indoorLocationManager:didUpdateLocations:")]
public void DidUpdateLocations(IALocationManager manager , IALocation[] locations)
{
IALocation loc = locations[locations.Length - 1];
if (mFloorPlan != null)
{
CoreGraphics.CGPoint cg = mFloorPlan.CoordinateToPoint(loc.Location.Coordinate);
this.map.Center = cg;
}
}
[Export("indoorLocationManager:didEnterRegion:")]
public void DidEnterRegion(IALocationManager manager, IARegion region)
{
if (region.Type != ia_region_type.FloorPlan)
Console.WriteLine("Region Changed to {0} " + region.Identifier);
else
{
FetchFloorPlan();
}

}

Answers

  • bryan1bryan1 IEMember ✭✭
    edited October 2

    Create a class that inherits from the delegate class, call the base constructor and override the methods you are interesting in.

    public class MyDelegate : IALocationManagerDelegate
    {
    public MyDelegate() : base()

    public override void DidEnterRegion(IALocationManager manager, IARegion region)
    {
        //maybe raise your own event here which can be captured in your location manager class below
    }
    

    }

    public class LocationManager()
    {
    private IALocationManager _locManager;
    private MyDelegate _delegate;
    public LocationManager()
    {
    _locManager = new IALocationManager();
    _delegate = new MyDelegate();

        _locManager.Delegate = _delegate;
    }
    

    }

    Calling the base constructor should call the delegates Init Method. Once you assign the delegate is there perhaps another initialize function you need to call. Or maybe thats all you need, i would set some breakpoints in the DidEnterRegion method you have overridden to see if they are hit.

  • NikheelNikheel INMember

    Hi,

    Thank you for your reply,

    What if I want to Implement delegate methods in ViewController class?
    How I can set delegate to viewcontroller class.

    Can't I direct override methods in ViewController Class?

    Something Like this

    public partial class ViewController : UIViewController, IIALocationManagerDelegate, IIAFetchTask
    {

        private IALocationManager manager;
    
        protected ViewController(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }
    
        [Export("indoorLocationManager:didUpdateLocations:")]
        public void DidUpdateLocations(IALocationManager manager, IALocation[] locations)
        {
            IALocation loc = locations[locations.Length - 1];
        }
    
        [Export("indoorLocationManager:didEnterRegion:")]
        public void DidEnterRegion(IALocationManager manager, IARegion region)
        {
            if (region.Type != ia_region_type.FloorPlan)
            {
                Console.WriteLine("Region Changed to {0} " + region.Identifier);
            }
            else
            {
                FetchFloorPlan();
            }
        }
    

    }

Sign In or Register to comment.