xamarin.ios在外部单击时请勿关闭UIAlertController

时间:2019-06-10 07:03:52

标签: xamarin xamarin.forms xamarin.ios uialertview

我正在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;
        }

1 个答案:

答案 0 :(得分:0)

没有与土司同等的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();
            }
        }
    }