繁体   English   中英

吐司通知Xamarin iOS

[英]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中才能显示警报。 这是因为UIAlertControllerUIViewController的子类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM