[英]Toast notification Xamarin iOS
我正在為Xamarin(C#)編寫iOS應用程序。
在iOS中,Android的“Toast notifications”相當於什么?
來自文檔 :
Toast在小彈出窗口中提供有關操作的簡單反饋。 它僅填充消息所需的空間量,並且當前活動保持可見和交互。 例如,在發送電子郵件之前導航遠離電子郵件會觸發“草稿保存”吐司,以便您知道以后可以繼續編輯。 超時后,Toasts會自動消失。
例如在Android(C#)中,我可以像這樣顯示它們:
Toast.MakeText(this, "Added " + counttext.Text +" pcs to cart" , ToastLength.Long).Show();
我如何為iOS編寫相同的內容?
謝謝你的回答。
在Xamarin iOS中,您必須使用自定義設計的UIView和動畫來實現相同的效果
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內置組件BigTed
https://components.xamarin.com/view/btprogresshud
BTProgressHUD.ShowToast("Hello from Toast");
您可以嘗試https://github.com/andrius-k/Toast
它可以作為nuget包Toast.ios使用
用法:
// import
using GlobalToast;
Toast.MakeToast("message").SetDuration(0.5).Show();
從iOS 9開始 ,建議使用UIAlertController類向用戶顯示反饋。
用法示例:
var alertController = UIAlertController.Create("Your Count", "Added 1 PC", UIAlertControllerStyle.Alert);
alertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, (action) => Console.WriteLine("OK Clicked.")));
PresentViewController(alertController, true, null);
要記住的一件事是,您必須在UIViewController中才能顯示警報。 這是因為UIAlertController是UIViewController的子類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.