Dependency Service With Event Handler In Xamarin

In the below article, we learned the basics of the dependency service

In this article, we are going to learn the following.

  • How to create an interface with event handler
  • Implementing the Dependency service in Droid
  • Calling the Dependency Service

How to create an interface with event handler

First, we need to create an interface with definitions of methods and event handler. For example, create an interface PositionChange which has the definition of one method Start() and positionChanged.

  1. public interface PositionChange  
  2.         {  
  3.                     void start ();  
  4.                     event EventHandler positionChanged;  
  5.         }   

In the PositionChangeclass code, positionChanged is an event which passed PositionEventArgs object, for that we need to create a model class with name “PositionEventArgs”.

  1. public class PositionEventArgs  
  2.       {  
  3.                string position {get; set;}  
  4.                public PositionEventArgs (string pos)  
  5.                    {  
  6.                     position = pos;  
  7.                    }  
  8.       }   

Implementing the Dependency service in Droid

Once a suitable interface has been designed, PositionChange interface must be implemented in the project for each platform you are targeting. For example, the following class implements the PositionChange interface on Android Phone.

As in the previous article, we need to declare paramterless constructor.

  1. [assembly: Xamarin.Forms.Dependency(typeof(CurrentLocationService_Android))]  
  2. namespace AndroidToPcl.Droid.Dependencies  
  3. {  
  4. public class CurrentLocationService_Android: PositionChange  
  5.       {  
  6.            public static CurrentLocationService_Android () {}  
  7.            public event EventHandler positionChanged;  
  8.            public void start ()  
  9.                 {  
  10.                          myself = this;  
  11.                          var context = Xamarin.Forms.Forms.Context;  
  12.                          Intent intent= new Intent(context, typeof(ChangeActivity));  
  13.                          context.StartActivity(intent);  
  14.                 }  
  15.            public void receivedNewPosition (CustomPosition pos)  
  16.                 {  
  17.                         positionChanged (this,new positionEventArgs(pos.update));  
  18.                 }  
  19.      }  
  20. }   


The registration is performed at the namespace level, not the class level. In the above code, context is changing from Xamarin.Forms to Android.

The below code represents the changing of context by calling native activity. We can navigate from one activity to another activity, and for that, we need to use Intent.

  1. Intent intent = new Intent(context, typeof(ChangeActivity));  
  2. context.StartActivity(intent);  

In the ChangeActivity activity code, I have changed the position manually which will trigger an event in the page.

  1. [Activity(Label = "ChangeActivity")]  
  2. public class ChangeActivity: Activity  
  3. {  
  4.       protected override void OnCreate (Bundle savedInstanceState)  
  5.           {  
  6.             base.OnCreate(savedInstanceState);  
  7.             CustomPosition pos = new CustomPosition();  
  8.             pos.update = "Finally value is updated";  
  9.             CurrentLocationService_Android.mySelf.receivedNewPosition(pos);  
  10.             this.StartActivity(typeof(MainActivity));  
  11.           }  
  12. }  

Call to Dependency Service

Once the project has been set up with a common interface and implementations for each platform, use Dependency Service to get the right implementation at runtime. The below code represents how to call the Dependency Service in the page.

  1. private void BtnClick_Clicked(object sender, EventArgs e)  
  2. {  
  3.  currentLocationService = DependencyService.Get();  
  4.  currentLocationService.start();  
  5.  currentLocationService.positionChanged += OnPositionChange;  
  6. }   

Below code is used to get the control when the position change event is triggered

  1. private void OnPositionChange (object sender, PositionEventArgs e)  
  2.    {  
  3.      Debug.WriteLine("Got the update in ContentPage from service ");  
  4.      // code to do  
  5.     }   

Finally, we will get the updated string in the PositionEventArgs object. By this method, we can get the data from the dependency service. We can achieve this with Message Center also.

That’s it; hope you enjoyed reading this article.


In this article, we have learned the following.

  • How to create an interface with event handler.
  • How to pass the data from native to forms using event handler.