Introduction
This article demonstrates how to make Toast Message or local message using Dependency Service in Xamarin.Forms applications.
This article demonstrates how to make Toast Message or local message using Dependency Service in Xamarin.Forms applications.
Android Output
UWP Output
Requirements
- Visual Studio 2017 update
- This article was prepared by a windows machine
- This sample project is targeted for Android, IOS, UWP and tested for Android and UWP
Let's start!
Step 1
You can create a new Xamarin.Forms app by going to File >> New >> Visual C# >> Cross Platform >>Cross Platform App(Xamarin.Forms or Xamarin.Native) and click OK.
You can create a new Xamarin.Forms app by going to File >> New >> Visual C# >> Cross Platform >>Cross Platform App(Xamarin.Forms or Xamarin.Native) and click OK.
(Eg - Project name - ToastMessage)
Step 2
After the project creation, add a new Toast Interface in Xamarin.Forms PCL project. For that, go to Solution Explorer >>click ToastMessage(PCL) project >> right click to select Add >> followed by select New Item >> Select Interface >> give the name as Toast and click ok. Here is the code for this interface.
After the project creation, add a new Toast Interface in Xamarin.Forms PCL project. For that, go to Solution Explorer >>click ToastMessage(PCL) project >> right click to select Add >> followed by select New Item >> Select Interface >> give the name as Toast and click ok. Here is the code for this interface.
C# Code
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace ToastMessage
- {
- public interface Toast
- {
- void Show(string message);
- }
- }
Step 4
Now, open MainPage.Xaml. For that, go to Solution Explorer >> ToastMessage(PCL) >> double-click to open design view of MainPage.xaml and the cCode is given below.
Now, open MainPage.Xaml. For that, go to Solution Explorer >> ToastMessage(PCL) >> double-click to open design view of MainPage.xaml and the cCode is given below.
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:ToastMessage"
- x:Class="ToastMessage.MainPage">
- <Button Text="Get Toast"
- HorizontalOptions="Center"
- VerticalOptions="Center"
- FontSize="Medium"
- x:Name="ToastButton"/>
- </ContentPage>
Step 5
Next, go to MainPage.xaml.cs . For that, go to Solution Explorer >>ToastMessage(PCL) >> click open MainPage.xaml.cs and add the following code.
Next, go to MainPage.xaml.cs . For that, go to Solution Explorer >>ToastMessage(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 ToastMessage
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
- protected override void OnAppearing()
- {
- ToastButton.Clicked += ToastButton_Clicked;
- }
- private void ToastButton_Clicked(object sender, EventArgs e)
- {
- DependencyService.Get<Toast>().Show("Toast Message");
- }
- }
- }
Step 6
Open Solution Explorer >> ToastMessage(PCL) >>right click and select New Item. In the popup window, select Cross Platform >>Class. This way, you can add a new class.
Open Solution Explorer >> ToastMessage(PCL) >>right click and select New Item. In the popup window, select Cross Platform >>Class. This way, you can add a new class.
Platform specific implementation
Xamarin.Android
In this step, create a new class named Toast_Android and add the following code.
C# Code
In this step, create a new class named Toast_Android and add the following code.
C# Code

Nguyen Tu KietPosted Jul 9, 2021, 11:51 AM
Grate article! It works for me
Shivraj PurohitPosted May 20, 2021, 1:04 PM
Hello, I am facing a issue. the Navigation.PopAsync() not working in iOS. After Dismiss the toast message.
Manish ChavdaPosted Dec 19, 2018, 12:18 AM
I am getting this error in iOS: "pushViewController:animated: called on <Xamarin_Forms_Platform_iOS_NavigationRenderer 0x7fe4f0253a00> while an existing transition or presentation is occurring; the navigation stack will not be updated." How can I fix it?
Munish APosted Jun 5, 2018, 9:38 PM
Nice article keep sharing bro......