loading...

Xamarin.Forms - How To Make SnackBar Using DependencyService step by step

logeshpalani98 profile image Logesh Palani Originally published at logeshpalani.blogspot.com on ・3 min read

Introduction

This article demonstrates how to make SnackBar in Android applications using DependencyService in Xamarin.Forms applications. It supports only Android devices and can't support IOS applications.

NuGet Package - search Xamarin.Android

  • Plugin.CurrentActivity

Xamarin.Android Output

| Xamarin |
| Xamarin.Droid |

Prerequisites

  • Visual Studio 2017
  • This sample project is targeted only for Android and tested Android devices

Let's start!

Step 1

You can create a new Xamarin.Forms application by going to File >> New >>Visual C# >> Cross-platform >> Cross-platform App (Xamarin.Forms or Xamarin.Native) and give solution name, then click OK .

(Eg - Project name - SnackBarDemo)

| Xamarin |
| fig 1 New Project |

Step 2

After the project creation, declare a DependencyService Interface in Xamarin.Forms PCL project, we could call this interface from our PCL projects. For that, go to Solution Explorer >> click SnackBarDemo(PCL) project >>right-click and select add >>followed by select New Item>> In the popup window, scroll down and select Interface >> give the name as SnackInterface then click OK. Here is the code for this interface.

C# Code

Step 3

Now, we are going to design a Button with a clicked event in MainPage.Xaml page. For that, go to Solution Explorer >> SnackBarDmeo(PCL) >> click open MainPage.Xaml and add the following code.

Xaml Code

Step 4

The next step would implement the DependencyService Interface in PCL project. For that, go to Solution Explorer >>SnackBarDemo(PCL) >> click open MainPage.xaml.cs and add the following code.

C# Code

Step 5

Afterwards, add the following NuGet Package to our Xamarin.Android project.

  • Plugin.CurrentActivity

| Xamarin |
| Fig 2 Nuget Package |

For that, open Solution Explorer and select your Xamarin.Andriod project solution. Right-click and select "Manage nuget Package". In the popup window, navigate "Browse" tab and browse "Plugin.CurrentActivity" and select following nuget package then install it.

Step 6

Next, we implement the interface in Xamarin.Android project. For that, go to Solution Explorer >> select SnackbarDemo.Android project and right-click and select Add Followed by selecting New Item. In the popup window, select class and give the name as SnackBar_Android.cs then add the following assembly and code.

C# Code

Step 7

Now, add another class named MainApplication and here is the code. In this code, the definition overrides the OnCreate method and calls "Init" method.

C# Code

Step 8

Finally, go to "Build" menu and click "Configure Manager". Here, configure your startup projects. Click "F5" or start to build and run your application.

After few seconds, the app will start runing on Android simulator or emulator and we will see the app working successfully.

| Xamarin |
| Output |

End result will look like below.


Full Source Code can be found here.

Posted on Mar 4 by:

Discussion

markdown guide