简体   繁体   English

吐司通知Xamarin iOS

[英]Toast notification Xamarin iOS

I'm writing an application for iOS in Xamarin (C#). 我正在为Xamarin(C#)编写iOS应用程序。

What is the equivalent of Android's 'toast notifications' in iOS? 在iOS中,Android的“Toast notifications”相当于什么?

From documentation : 来自文档

A toast provides simple feedback about an operation in a small popup. Toast在小弹出窗口中提供有关操作的简单反馈。 It only fills the amount of space required for the message and the current activity remains visible and interactive. 它仅填充消息所需的空间量,并且当前活动保持可见和交互。 For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. 例如,在发送电子邮件之前导航远离电子邮件会触发“草稿保存”吐司,以便您知道以后可以继续编辑。 Toasts automatically disappear after a timeout. 超时后,Toasts会自动消失。

For example in Android (C#) I can show them like this: 例如在Android(C#)中,我可以像这样显示它们:

Toast.MakeText(this, "Added " + counttext.Text +" pcs to cart" , ToastLength.Long).Show();

How do I write the same for iOS? 我如何为iOS编写相同的内容?

Thank's for answer. 谢谢你的回答。

In Xamarin iOS you have to use custom designed UIView with animation to achieve the same effect 在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();
    }

Use the Xamarin built-in component BigTed 使用Xamarin内置组件BigTed

https://components.xamarin.com/view/btprogresshud https://components.xamarin.com/view/btprogresshud

BTProgressHUD.ShowToast("Hello from Toast");

You can try https://github.com/andrius-k/Toast 您可以尝试https://github.com/andrius-k/Toast

It is available as nuget package Toast.ios 它可以作为nuget包Toast.ios使用
usage: 用法:

// import
using GlobalToast;
Toast.MakeToast("message").SetDuration(0.5).Show();

As of iOS 9 , the UIAlertController class is recommended for displaying feedback to the user. iOS 9开始 ,建议使用UIAlertController类向用户显示反馈。

Example usage: 用法示例:

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);

One thing to bare in mind is that you must be in a UIViewController in order to present the alert. 要记住的一件事是,您必须在UIViewController中才能显示警报。 This is due to the fact that a UIAlertController is a subclass of a UIViewController . 这是因为UIAlertControllerUIViewController的子类。

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

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