首页 > 解决方案 > xamarin.ios 在外部单击时不会关闭 UIAlertController

问题描述

我正在从事xamarin.forms项目工作,但遇到了一个问题。

UIAlertController我希望无论何时(以 android 术语Toast)显示主屏幕都保持启用状态。对我来说,这两件事都很重要。

显示警报时,背景视图中的按钮应该是可点击的。并且由于需要显示重要消息,因此警报也应在给定时间内并行显示。

在 android 中,Toast不会干扰主屏幕上的用户交互。我可以在 iOS 中有同样的工作吗?

这是我在依赖服务中尝试过的。

void ShowAlert(string message, double seconds)
        {
            try
            {
                if (alert == null && alertDelay == null)
                {
                    alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                           DismissMessage();
                        });
                    });

                    Device.BeginInvokeOnMainThread(() =>
                    {
                        try
                        {
                            alert = UIAlertController.Create("", message, UIAlertControllerStyle.Alert);
                            alert.View.UserInteractionEnabled = true;

                            topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController).PresentViewController(alert, true, () =>
                            {
                                UITapGestureRecognizer tap = new UITapGestureRecognizer(() => { });   // I have tried this but nothing happens
                                alert.View.Superview.Subviews[0].AddGestureRecognizer(tap);
                            });
                        }
                        catch (Exception ex)
                        {
                            var Error = ex.Message;
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                var Error = ex.Message;
            }
        }

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

        UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
        {
            try
            {
                if (rootViewController is UITabBarController)
                {
                    UITabBarController tabBarController = (UITabBarController)rootViewController;
                    return topViewControllerWithRootViewController(tabBarController.SelectedViewController);
                }
                else if (rootViewController is UINavigationController)
                {
                    UINavigationController navigationController = (UINavigationController)rootViewController;
                    return topViewControllerWithRootViewController(navigationController.VisibleViewController);
                }
                else if (rootViewController.PresentedViewController != null)
                {
                    UIViewController presentedViewController = rootViewController.PresentedViewController;
                    return topViewControllerWithRootViewController(presentedViewController);
                }
            }
            catch (Exception)
            {
            }
            return rootViewController;
        }

标签: xamarinxamarin.formsxamarin.iosuialertview

解决方案


没有 iOS 原生的等价物。但是,这里有很多方法可以做到这一点:Toast equivalent for Xamarin Forms

那里提到的众多解决方案之一将起作用,如果您正在寻找本机解决方案,您可以显示一个警报,该警报会在指定时间后自行关闭,如下所示:

    public class MessageIOS
    {
        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();
            }
        }
    }

推荐阅读