Xamarin Forms 的 Toast 等价物 [英] Toast equivalent for Xamarin Forms

查看:21
本文介绍了Xamarin Forms 的 Toast 等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用 Xamarin Forms(不是 Android 或 iOS 特定的)来弹出一个弹出窗口,就像 Android 对 Toast 所做的那样,不需要用户交互并在(短)一段时间后消失?

Is there any way using Xamarin Forms (not Android or iOS specific) to have a pop-up, like Android does with Toast, that needs no user interaction and goes away after a (short) period of time?

通过搜索,我看到的都是需要用户点击才能消失的警报.

From searching around all I'm seeing are alerts that need user clicks to go away.

推荐答案

对此有一个简单的解决方案.通过使用 DependencyService,您可以轻松获得 Toast- 类似于 Android 和 iOS 中的方法.

There is a simple solution for this. By using the DependencyService you can easily get the Toast-Like approach in both Android and iOS.

在您的通用包中创建一个接口.

public interface IMessage
{
    void LongAlert(string message);
    void ShortAlert(string message);
}

Android 部分

[assembly: Xamarin.Forms.Dependency(typeof(MessageAndroid))]
namespace Your.Namespace
{
    public class MessageAndroid : IMessage
    {
        public void LongAlert(string message)
        {
            Toast.MakeText(Application.Context, message, ToastLength.Long).Show();
        }

        public void ShortAlert(string message)
        {
            Toast.MakeText(Application.Context, message, ToastLength.Short).Show();
        }
    }
}

iOS 部分

在 iOs 中没有像 Toast 这样的原生解决方案,所以我们需要实现自己的方法.

In iOs there is no native solution like Toast, so we need to implement our own approach.

[assembly: Xamarin.Forms.Dependency(typeof(MessageIOS))]
namespace Bahwan.iOS
{
    public class MessageIOS : IMessage
    {
        const double LONG_DELAY = 3.5;
        const double SHORT_DELAY = 2.0;

        NSTimer alertDelay;
        UIAlertController alert;

        public void LongAlert(string message)
        {
            ShowAlert(message, LONG_DELAY);
        }
        public void ShortAlert(string message)
        {
            ShowAlert(message, SHORT_DELAY);
        }

        void ShowAlert(string message, double seconds)
        {
            alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
            {
                dismissMessage();
            });
            alert = UIAlertController.Create(null, message, UIAlertControllerStyle.Alert);
            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
        }

        void dismissMessage()
        {
            if (alert != null)
            {
                alert.DismissViewController(true, null);
            }
            if (alertDelay != null)
            {
                alertDelay.Dispose();
            }
        }
    }
}

请注意,在每个平台中,我们都必须向 DependencyService 注册我们的类.

Please note that in each platform, we have to register our classes with DependencyService.

现在您可以在我们项目的任何地方访问 Toast 服务.

Now you can access out Toast service in anywhere in our project.

DependencyService.Get<IMessage>().ShortAlert(string message); 
DependencyService.Get<IMessage>().LongAlert(string message);

这篇关于Xamarin Forms 的 Toast 等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆