问题描述
我正在使用 PCL 项目以 xamarin 形式创建一个应用程序.我想在两次回按时实现 Toast 通知,仅适用于 android 和 ios.对于 android 我试过 -
long doublePressInterval_ms = 300;日期时间 lastPressTime = DateTime.MinValue;DateTime pressTime = DateTime.Now;if ((pressTime - lastPressTime).TotalMilliseconds
但它显示错误无法将页面转换为 Android 上下文.如何在我的 pcl 项目中获取 adnroid 上下文?
我尝试了
解决方案
在 Xamarin Android 中你可以像往常一样显示
Toast.MakeText(this,"toast message", ToastLength.Long).Show();
在 Xamarin iOS 中,您必须使用自定义设计的带有动画的 UIView 才能达到相同的效果
public void ShowToast(String message, UIView view){UIView 残留视图 = view.ViewWithTag(1989);如果(剩余视图!= null)残留视图.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 = 消息;lblMsg.TextColor = UIColor.White;lblMsg.TextAlignment = UITextAlignment.Center;viewBack.Center = view.Center;viewBack.AddSubview(lblMsg);view.AddSubview(viewBack);转角(viewBack);UIView.BeginAnimations("Toast");UIView.SetAnimationDuration(3.0f);viewBack.Alpha = 0.0f;UIView.CommitAnimations();}
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.
解决方案
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();
}
这篇关于Xamarin 表单:Android & 中的 Toast 通知IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!