Uncategorized

Dependency Service with Event Handlers

In this below article, we have learnt 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 having the definition of one method Start() & positionChanged

    public interface PositionChange
        {
                    void start ();
                    event EventHandler positionChanged;
        }

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

public class PositionEventArgs
      {
               string position {get; set;}
               public PositionEventArgs (string pos)
                   {
                    position = pos;
                   }
      }

Implementing the Dependency service in Droid

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

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


[assembly: Xamarin.Forms.Dependency(typeof(CurrentLocationService_Android))]

namespace AndroidToPcl.Droid.Dependencies
{
public class CurrentLocationService_Android: PositionChange
      {
           public static CurrentLocationService_Android () {}
           public event EventHandler positionChanged;
           public void start ()
                {
                         myself = this;
                         var context = Xamarin.Forms.Forms.Context;
                         Intent intent= new Intent(context, typeof(ChangeActivity));
                         context.StartActivity(intent);
                }
           public void receivedNewPosition (CustomPosition pos)
                {
                        positionChanged (this,new positionEventArgs(pos.update));
                }
     }
}

Note: that the registration is performed at the namespace level, not the class level. In the above code, context is changing from Xamarin.Forms to Android.
Below code is representing the changing of context by calling native activity. We can navigate from one activity to another activity we need to use Intent.

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


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


[Activity(Label = “ChangeActivity”)]

public class ChangeActivity: Activity
{
      protected override void OnCreate (Bundle savedInstanceState)
          {
            base.OnCreate(savedInstanceState);
            CustomPosition pos = new CustomPosition();
            pos.update = “Finally value is updated”;
            CurrentLocationService_Android.mySelf.receivedNewPosition(pos);
            this.StartActivity(typeof(MainActivity));
          }
}


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. Below code represents how to call the dependency service in the page

private void BtnClick_Clicked(object sender, EventArgs e)
{
 currentLocationService = DependencyService.Get();
 currentLocationService.start();
 currentLocationService.positionChanged += OnPositionChange;
}

Below code is use get the control when the position changed event is trigger

 private void OnPositionChange (object sender, PositionEventArgs e)
    {
      Debug.WriteLine(“Got the update in ContentPage from service “);
      // code to do
     }

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

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

Summary:

In this article, we have learnt the following

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