Trigger and handle DependencyService event

I am trying to trigger an event in a dependency service, and handle this event in a helper that accesses this service. Whenever I call my event, and pass in the event arguments, it throws an exception:

System.NullReferenceException has been thrown
Object reference not set to an instance of an object.

Do I need to instantiate this event further before its use?

Inside this dependency service, inheriting from my interface, I define the event:

internal class NfcImplementation : Java.Lang.Object, INfc, NfcAdapter.IReaderCallback
{
    public event TagDetectedDelegate TagDetected;
    ...
}

This TagDetectedDelegate is defined in this interface:

public delegate void TagDetectedDelegate(String payload);

public interface INfc
{
   event TagDetectedDelegate TagDetected;
   ...
} 

And later when I wish to invoke the event:
TagDetected(tag_content); // This line triggers the NullReferenceException

I also tried this syntax, but it produces the same issue:
TagDetected.Invoke(tag_content);

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Why do you have to use a DependencyService to read an NFC tag? I am curious...

  • Chris_xamChris_xam Member ✭✭

    @AlessandroCaliaro said:
    Why do you have to use a DependencyService to read an NFC tag? I am curious...

    I'm implementing a cross platform NFC solution that has an iOS dependency service and an Android dependency service. Both of these services have an event, accessible through the interface. This means in my view model I can just listen to the interface event and handle it as it comes in, regardless of the platform it came from.

  • Chris_xamChris_xam Member ✭✭

    Thanks, that is a functional alternate way to do this :smile:

Sign In or Register to comment.