A simple way of showing some notifications inside your Xamarin.Forms application. In windows phone world we call them "Toasts".
Android implementation is based on https://github.com/keyboardsurfer/Crouton with several changes and ported to C# without bindings.
iOS implementation is based on https://github.com/terryworona/TWMessageBarManager with several changes (thanks to prashantvc with his https://github.com/prashantvc/Xamarin.iOS-MessageBar)
Unlike others WP8's version supports multiply toasts (can be limited to 1).
- Available on NuGet: https://www.nuget.org/packages/Toasts.Forms.Plugin
- Install into your PCL project and Client projects.
In your iOS, Android, and Windows Phone projects please call:
Xamarin.Forms.Init();//platform specific init
ToastNotificatorImplementation.Init(); //you can pass additional parameters here
You must do this AFTER you call Xamarin.Forms.Init(); By the way, toasts on each platform support customization - you can pass your own style (custom renderer) as an argument in Init() method.
Use dependency service in order to resolve IToastNotificator
var notificator = DependencyService.Get<IToastNotificator>();
bool tapped = await notificator.Notify(ToastNotificationType.Error,
"Error", "Something went wrong", TimeSpan.FromSeconds(2));
On all three platforms you can completely override toast UI. However, there is also an easy way to add a new status with custom icon and background, let's take a look on a Windows Phone example. In this example we want to add a few more types of messages with custom icons. Our code in PCL will look like this:
var notificator = DependencyService.Get<IToastNotificator>();
bool tapped = await notificator.Notify(
type: ToastNotificationType.Custom,
title: "Level up!",
description: "Congratulations!",
duration: TimeSpan.FromSeconds(2),
context: MyCustomTypes.LevelUp);
So we have just set the type to Custom and passed an additional argument to Object context (last argument) - our custom enum MyCustomTypes.LevelUp. Now we have to configure default renderer (or replace it by your own if you want to add more changes to the layout):
Xamarin.Forms.Forms.Init();
ToastNotificatorImplementation.Init(stackSize: 2,
customRenderer: new DefaultToastLayoutRenderer(
context =>
{
switch ((MyCustomTypes)context)
{
case MyCustomTypes.LevelUp:
return new BitmapImage(new Uri("level_up.png", UriKind.Relative));
...
}
},
context => new SolidColorBrush(Colors.Magenta)));
iOS and Android api also have similar extensibility
Thanks!
Licensed under MIT