Xamarin iOS的Toast通知

5
我正在使用Xamarin(C#)编写iOS应用程序。
在iOS中,与Android的“toast通知”相当的是什么?
根据文档
烤面包干提供有关操作的简单反馈。它仅填充所需的消息和当前活动仍然可见和交互。例如,在发送电子邮件之前导航到其他地方会触发“已保存草稿”的烤面包干,以让您知道可以稍后继续编辑。烤面包干会在超时后自动消失。
例如,在Android(C#)中,我可以像这样显示它们:
Toast.MakeText(this, "Added " + counttext.Text +" pcs to cart" , ToastLength.Long).Show();

我该如何在iOS上编写相同的代码?

谢谢回答。

4个回答

11
在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();
    }

如何在可移植界面中定义UIView并在iOS项目中实现,同时我想从可移植界面显示toast通知? - Sonali
这个不是要从Portable中显示。你可以从Xamarin.iOS侧面展示它。 - Durai Amuthan.H
假设我已经在可移植界面中进行了实现,并将其实现到了iOS项目中,那么我该如何将UIview传递给它? - Sonali
@Sonali - 我以前从未尝试过这个... Portable 应该是跨平台的,因此在那里使用 iOS 特定代码将与其目的相矛盾。您可以在 xamarin.iOS 端使用它,我认为这不会很难。 - Durai Amuthan.H
我找到的最简单的解决方案是对roundTheCorner使用viewBack.Layer.CornerRadius=10; viewBack.Layer.MasksToBounds=true; - Entretoize

4

2
你可以尝试使用https://github.com/andrius-k/Toast。它作为nuget包Toast.ios提供。
用法:
// import
using GlobalToast;
Toast.MakeToast("message").SetDuration(0.5).Show();

尝试使用这个,但是出现了使用Toast的错误。获取到了空引用。 - Swati

0

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的子类。


1
“在iOS中,与Android的‘toast通知’相当的是什么?”很抱歉,但这不是等价物,要将其关闭,您必须执行一个操作。 - Paolo Mastrangelo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接