- 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)
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
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace SnackBarDemo
- {
- public interface SnackInterface
- {
- void SnackbarShow(string message);
- }
- }
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
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:SnackBarDemo"
- x:Class="SnackBarDemo.MainPage"
- Title="SnackBar Demo Page"
- BackgroundColor="AliceBlue"
- >
-
- <Button Text="Show SnackBar!"
- VerticalOptions="Center"
- HorizontalOptions="Center"
- Clicked="Button_OnClicked"/>
-
- </ContentPage>
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
-
- namespace SnackBarDemo
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
-
- private void Button_OnClicked(object sender, EventArgs e)
- {
- DependencyService.Get<SnackInterface>().SnackbarShow("Am SnackBar in Xamarin.Android");
- }
- }
- }
Step 5
Afterwards, add the following NuGet Package to our Xamarin.Android project.
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- using Android.App;
- using Android.Content;
- using Android.OS;
- using Android.Runtime;
- using Android.Support.Design.Widget;
- using Android.Views;
- using Android.Widget;
- using Plugin.CurrentActivity;
- using SnackBarDemo.Droid;
- [assembly:Xamarin.Forms.Dependency(typeof(SnackBar_Android))]
-
- namespace SnackBarDemo.Droid
- {
- public class SnackBar_Android : SnackInterface
- {
- public void SnackbarShow(string message)
- {
- Activity activity = CrossCurrentActivity.Current.Activity;
- Android.Views.View view = activity.FindViewById(Android.Resource.Id.Content);
- Snackbar.Make(view,message,Snackbar.LengthLong).Show();
- }
- }
- }
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
- using System;
- using Android.App;
- using Android.Runtime;
- using Plugin.CurrentActivity;
-
- namespace SnackBarDemo.Droid
- {
- [Application]
- public class MainApplication : Application
- {
- public MainApplication(IntPtr handle, JniHandleOwnership transer)
- : base(handle, transer)
- {
- }
-
- public override void OnCreate()
- {
- base.OnCreate();
- CrossCurrentActivity.Current.Init(this);
- }
-
- }
- }
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.
End result will look like below.
Full Source Code can be found
here.