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.
- public interface PositionChange
- {
- void start ();
- event EventHandler positionChanged;
- }
In the PositionChangeclass 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 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.
- [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
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.
- 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. The 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 used to get the control when the position change event is triggered
- private void OnPositionChange (object sender, PositionEventArgs e)
- {
- Debug.WriteLine("Got the update in ContentPage from service ");
-
- }
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.
Summary
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.