What is a Dependency Service in Xamarin.Forms ?

Hi Guys,

Today, I am going to teach you about Dependency Services in Xamarin.Forms.

What is a dependency service ?

Xamarin.Forms is a PCL package. So you cannot access all the native APIs on each platforms.For an example, Can you show a Toast message in Android from your PCL project ?

Note: PCL project is the place where you define your UI in Xamarin.Forms App.

Answer is NO…

If NO then whats the solution ?

The solution is to write a Dependency Service.

Dependency Service is a feature in Xamarin.Forms where you can execute/call/invoke a function/method in the native platform.

battery_diagram

As you see in the above diagram, there is a Dependency Service written to get the battery status from Native platform to PCL code.

Now, Lets write a small Dependency Service… 🙂

  1. Create a project called DSToast.

screen-shot-2017-02-18-at-8-57-17-pm

2. Create a HomePage and set it as the MainPage in App.xaml.

screen-shot-2017-02-18-at-9-01-26-pm

3. Add a button to HomePage and set the click event handler.

HomePage.xaml

<?xml version=1.0 encoding=UTF8?>
<ContentPage xmlns=http://xamarin.com/schemas/2014/forms xmlns:x=http://schemas.microsoft.com/winfx/2009/xaml x:Class=DSToast.HomePage>
    <ContentPage.Content>
        <Button Text=Show Message Clicked=OnClick HorizontalOptions=Center VerticalOptions=Center/>
    </ContentPage.Content>
</ContentPage>

HomePage.xaml.cs

using System;
using Xamarin.Forms;

namespace DSToast
{
    public partial class HomePage : ContentPage
    {
        public HomePage()
        {
            InitializeComponent();
        }

        private void OnClick(Object sender, EventArgs e)
        { 
            
        }
    }
}

4. Now create following dependency interface and classes.

  • INativeMsg.cs in PCL project.
  • NativeMsg.cs in Android.
  • NativeMsg.cs in iOS.

screen-shot-2017-02-18-at-9-12-48-pm

INativeMsg.cs in PCL project

namespace DSToast
{
    public interface INativeMsg
    {
        void ShowMsg(string msg);
    }
}

NativeMsg.cs in .Droid project

using DSToast.Droid;
using Xamarin.Forms;
using Android.Widget;

[assembly: Dependency(typeof(NativeMsg))]
namespace DSToast.Droid
{
    public class NativeMsg : INativeMsg
    {
        public void ShowMsg(string msg)
        {
            Toast.MakeText(Forms.Context, msg, ToastLength.Long).Show();
        }
    }
}

NativeMsg.cs in .iOS project

using DSToast.iOS;
using Xamarin.Forms;
using UIKit;

[assembly: Dependency(typeof(NativeMsg))]
namespace DSToast.iOS
{
    public class NativeMsg : INativeMsg
    {
        public void ShowMsg(string msg)
        {
            var alert = new UIAlertView(Message, msg, null, OK,null);
            alert.Show();
        }
    }
}

Now, our Dependency Service is ready to use. Let’s use… 🙂

5. Go to click event handler in PCL project and call the Dependency Service like below.

DependencyService.Get<INativeMsg>().ShowMsg(I am a Native Message!);

screen-shot-2017-02-18-at-9-34-27-pm

6. Now compile and run on each platforms.

Android:

screenshot_20170218-212312

iOS:

simulator-screen-shot-feb-18-2017-9-30-05-pm

Isn’t that cool ?   🙂

Source code available in https://github.com/hiranpeiris/Xamarin-DSToast

Happy coding… 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s