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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With