问题
Hi I am creating an app in xamarin forms using PCL project. I want to implement Toast notification on twice back press only for android and ios. For android I tried -
long doublePressInterval_ms = 300;
DateTime lastPressTime = DateTime.MinValue;
DateTime pressTime = DateTime.Now;
if ((pressTime - lastPressTime).TotalMilliseconds <= doublePressInterval_ms)
{
if(Device.OS == TargetPlatform.Android)
{
Java.Lang.JavaSystem.Exit(0);
}
}
else
{
Android.Widget.Toast.MakeText(this, string_name, ToastLength.Long).Show();
}
lastPressTime = pressTime;
return false;
But it shows error Cannot convert Page to Android Context. How can I get adnroid context in my pcl project?
I tried Toast Notification Plugin for Xamarin but it says .Net version is incompatible.
回答1:
In Xamarin Android you can show as usual like
Toast.MakeText(this,"toast message", ToastLength.Long).Show();
In Xamarin iOS you have to use custom designed UIView with animation to achieve the same effect
public void ShowToast(String message, UIView view)
{
UIView residualView = view.ViewWithTag(1989);
if (residualView != null)
residualView.RemoveFromSuperview();
var viewBack = new UIView(new CoreGraphics.CGRect(83, 0, 300, 100));
viewBack.BackgroundColor = UIColor.Black;
viewBack.Tag = 1989;
UILabel lblMsg = new UILabel(new CoreGraphics.CGRect(0, 20, 300, 60));
lblMsg.Lines = 2;
lblMsg.Text = message;
lblMsg.TextColor = UIColor.White;
lblMsg.TextAlignment = UITextAlignment.Center;
viewBack.Center = view.Center;
viewBack.AddSubview(lblMsg);
view.AddSubview(viewBack);
roundtheCorner(viewBack);
UIView.BeginAnimations("Toast");
UIView.SetAnimationDuration(3.0f);
viewBack.Alpha = 0.0f;
UIView.CommitAnimations();
}
回答2:
You can refer to Toast Notifications for Xamarin Forms, and here is the sample code.
Basically it uses DependencyService to implement ToastNotification
on each platform, while each platform has its own implementation for toasting a notification.
You can follow the guide to complete the work, the only problem I met with this guide is the installation of this Toasts.Forms.Plugin. It is possible that you may meet this exception when you install this package on PCL:
Could not install package 'Toasts.Forms.Plugin 3.1.2'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile259', but the package does not contain any assembly references or content files that are compatible with that framework.
To solve this issue, you can right click the PCL and "Unload Project", then right click on PCL again and choose "Edit NAMESPACE.proj", replace the code <TargetFrameworkProfile>Profile259</TargetFrameworkProfile>
with <TargetFrameworkProfile>Profile111</TargetFrameworkProfile>
, save this file and reload this project. After changing this TargetFrameworkProfile
, this plugin can be successfully installed on PCL.
回答3:
I have created the documentation for the Toast Notification in Xamarin.Forms (Portable) project. However, I didn't get the time to work on iPhone application (frankly I can't event checked in iPhone as I don't have Mac ;) ), but you can use it for the android application.
It is using the Inbuilt feature and you don't have to download any external plugin for this.
Link: https://docs.google.com/document/d/1C9mrsxvww3RIrm_BrtDWfKZrp6cAvZVqevewznIUHwI/edit?usp=sharing
Sample Code: https://github.com/imchandresh/ToastMessage/tree/master
Thank You.
回答4:
I used the below. Demo found Here
using Foundation;
using UIKit;`
using Xamarin.Forms;
using XamStart.Interfaces;
using XamStart.iOS.DependencyServices;
using XamStart.Models;
[assembly: Dependency(typeof(ToastService))]
namespace XamStart.iOS.DependencyServices
{
public class ToastService : IToastService
{
// Code stolen from here: http://sezeromer.com/xamarin-forms-ios-toast-mesaj/
const double LONG_DELAY = 3.5;
const double SHORT_DELAY = 2.0;
NSTimer alertDelay;
UIAlertController alert;
public void CookIt(string message, MyToastLength length)
{
var toastLength = (length == MyToastLength.Long) ? LONG_DELAY : SHORT_DELAY;
alertDelay = NSTimer.CreateRepeatingScheduledTimer(toastLength, (obj) =>
{
MesajReddet();
});
alert = UIAlertController.Create(null, message, UIAlertControllerStyle.Alert);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
}
void MesajReddet()
{
if (alert != null)
{
alert.DismissViewController(true, null);
}
if (alertDelay != null)
{
alertDelay.Dispose();
}
}
}
}
回答5:
public interface IToast
{
void Message(string message);
}
[assembly: Xamarin.Forms.Dependency(typeof(STToast))]
namespace MasterDetail.Droid.Renderers
{
public class STToast: IToast
{
public void Message(string message)
{
Toast.MakeText(Application.Context, message, ToastLength.Long).Show();
}
}
}
[assembly: Xamarin.Forms.Dependency(typeof(STToast))]
namespace MasterDetail.iOS.Renderers
{
class STToast: IToast
{
const double LONG_DELAY = 3.5;
const double SHORT_DELAY = 2.0;
NSTimer alertDelay;
UIAlertController alert;
public void Message(string message)
{
ShowAlert(message, LONG_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();
}
}
}
}
//MainPage
DependencyService.Get<IToast>().Message("Toast Message");
来源:https://stackoverflow.com/questions/42082807/xamarin-forms-toast-notification-in-android-ios