Introduction

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.
(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.
C# Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ToastMessage
  5. {
  6. public interface Toast
  7. {
  8. void Show(string message);
  9. }
  10. }
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.
Xaml Code
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:local="clr-namespace:ToastMessage"
  5. x:Class="ToastMessage.MainPage">
  6. <Button Text="Get Toast"
  7. HorizontalOptions="Center"
  8. VerticalOptions="Center"
  9. FontSize="Medium"
  10. x:Name="ToastButton"/>
  11. </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.
C# Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. namespace ToastMessage
  8. {
  9. public partial class MainPage : ContentPage
  10. {
  11. public MainPage()
  12. {
  13. InitializeComponent();
  14. }
  15. protected override void OnAppearing()
  16. {
  17. ToastButton.Clicked += ToastButton_Clicked;
  18. }
  19. private void ToastButton_Clicked(object sender, EventArgs e)
  20. {
  21. DependencyService.Get<Toast>().Show("Toast Message");
  22. }
  23. }
  24. }
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.
Platform specific implementation
Xamarin.Android

In this step, create a new class named Toast_Android and add the following code.

C# Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using Android.App;
  7. using Android.Content;
  8. using Android.OS;
  9. using Android.Runtime;
  10. using Android.Views;
  11. using Android.Widget;
  12. using ToastMessage.Droid;
  13. [assembly: Xamarin.Forms.Dependency(typeof(Toast_Android))]
  14. namespace ToastMessage.Droid
  15. {
  16. public class Toast_Android : Toast
  17. {
  18. public void Show(string message)
  19. {
  20. Android.Widget.Toast.MakeText(Android.App.Application.Context,message,ToastLength.Long).Show();
  21. }
  22. }
  23. }
Xamarin.IOS

Now, add another class named Toast_IOS and here is code for this class.
C# Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using Foundation;
  7. using ToastMessage.iOS;
  8. using UIKit;
  9. [assembly:Xamarin.Forms.Dependency(typeof(Toast_IOS))]
  10. namespace ToastMessage.iOS
  11. {
  12. public class Toast_IOS : Toast
  13. {
  14. const double LONG_DELAY = 3.5;
  15. NSTimer alertDelay;
  16. UIAlertController alert;
  17. public void Show(string message)
  18. {
  19. ShowAlert(message, LONG_DELAY);
  20. }
  21. void ShowAlert(string message, double seconds)
  22. {
  23. alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
  24. {
  25. dismissMessage();
  26. });
  27. alert = UIAlertController.Create(null, message, UIAlertControllerStyle.Alert);
  28. UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
  29. }
  30. void dismissMessage()
  31. {
  32. if (alert != null)
  33. {
  34. alert.DismissViewController(true, null);
  35. }
  36. if (alertDelay != null)
  37. {
  38. alertDelay.Dispose();
  39. }
  40. }
  41. }
  42. }
Xamarin.UWP

Similarly, create another class named Toast_UWP and insert the code given below.
C# Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Windows.Data.Xml.Dom;
  7. using Windows.UI.Notifications;
  8. using ToastMessage.UWP;
  9. [assembly:Xamarin.Forms.Dependency(typeof(Toast_UWP))]
  10. namespace ToastMessage.UWP
  11. {
  12. class Toast_UWP : Toast
  13. {
  14. public void Show(string message)
  15. {
  16. ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText01;
  17. XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
  18. XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
  19. toastTextElements[0].AppendChild(toastXml.CreateTextNode(message));
  20. XmlNodeList toastImageAttributes = toastXml.GetElementsByTagName("image");
  21. ((XmlElement)toastImageAttributes[0]).SetAttribute("src", "ms-appx:///Assets/Logo.scale-240.png");
  22. ((XmlElement)toastImageAttributes[0]).SetAttribute("alt", "logo");
  23. IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
  24. ((XmlElement)toastNode).SetAttribute("duration", "short");
  25. var toastNavigationUriString = "#/MainPage.xaml?param1=12345";
  26. var toastElement = ((XmlElement)toastXml.SelectSingleNode("/toast"));
  27. toastElement.SetAttribute("launch", toastNavigationUriString);
  28. ToastNotification toast = new ToastNotification(toastXml);
  29. ToastNotificationManager.CreateToastNotifier().Show(toast);
  30. }
  31. }
  32. }
Step 7

Click 'F5' or "Build" to "Run" your application. Running this project, you will have a result like below.
Xamarin.Android
Xamarin.UWP
Finally, we have successfully created a Xamarin.Forms ToastMessage application.

Please download the source code here.