Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toast equivalent for Xamarin Forms

Is there any way using Xamarin Forms (not Android or iOS specific) to have a pop-up, like Android does with Toast, that needs no user interaction and goes away after a (short) period of time?

From searching around all I'm seeing are alerts that need user clicks to go away.

like image 291
Jimmy Avatar asked Feb 08 '16 21:02

Jimmy


People also ask

What is xamarin essentials?

Xamarin. Essentials provides a single cross-platform API that works with any iOS, Android, or UWP application that can be accessed from shared code no matter how the user interface is created. See the platform & feature support guide for more information on supported operating systems.


1 Answers

There is a simple solution for this. By using the DependencyService you can easily get the Toast-Like approach in both Android and iOS.

Create an interface in your common package.

public interface IMessage {     void LongAlert(string message);     void ShortAlert(string message); } 

Android section

[assembly: Xamarin.Forms.Dependency(typeof(MessageAndroid))] namespace Your.Namespace {     public class MessageAndroid : IMessage     {         public void LongAlert(string message)         {             Toast.MakeText(Application.Context, message, ToastLength.Long).Show();         }              public void ShortAlert(string message)         {             Toast.MakeText(Application.Context, message, ToastLength.Short).Show();         }     } } 

iOS section

In iOs there is no native solution like Toast, so we need to implement our own approach.

[assembly: Xamarin.Forms.Dependency(typeof(MessageIOS))] namespace Your.Namespace {     public class MessageIOS : IMessage     {         const double LONG_DELAY = 3.5;         const double SHORT_DELAY = 2.0;          NSTimer alertDelay;         UIAlertController alert;          public void LongAlert(string message)         {             ShowAlert(message, LONG_DELAY);         }         public void ShortAlert(string message)         {             ShowAlert(message, SHORT_DELAY);         }          void ShowAlert(string message, double seconds)         {             alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>             {                 dismissMessage();             });             alert = UIAlertController.Create(null, message, UIAlertControllerStyle.Alert);             UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);         }          void dismissMessage()         {             if (alert != null)             {                 alert.DismissViewController(true, null);             }             if (alertDelay != null)             {                 alertDelay.Dispose();             }         }     } } 

Please note that in each platform, we have to register our classes with DependencyService.

Now you can access out Toast service in anywhere in our project.

DependencyService.Get<IMessage>().ShortAlert(string message);  DependencyService.Get<IMessage>().LongAlert(string message); 
like image 54
Alex Chengalan Avatar answered Sep 23 '22 13:09

Alex Chengalan