【问题标题】:can not show alertcontroller when page is displayed using PushModalAsync使用 PushModalAsync 显示页面时无法显示警报控制器
【发布时间】:2019-05-20 15:56:36
【问题描述】:

我正在尝试使用依赖服务在 xamarin.forms 项目的 android 和 iOS 中显示 toast 消息。在 iOS 项目中,消息显示在 MainPage 或 NavigationPage 上。但是当我使用 PushModalAsync 在按钮单击上导航第二页时,不会显示消息。

我如何浏览页面

public LoginPage()
{
Device.BeginInvokeOnMainThread(() =>
                {
                    CustomToast.LongMessage("Hiiiiii");  // Message shown
                });

 Navigation.PushModalAsync(new RegisterPage());   //Doesn't show
//var reg = new RegisterPage();
//Application.Current.MainPage = reg;  // toast shown here
}

iOS 中的 alertController 代码:

        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)
        {
            try
            {
                if (alert == null && alertDelay == null)
                {
                    alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            DismissMessage();
                        });
                    });

                    Device.BeginInvokeOnMainThread(() =>
                    {
                        try
                        {
                            alert = UIAlertController.Create("", message, UIAlertControllerStyle.ActionSheet);
                            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
                        }
                        catch (Exception ex)
                        {
                            var Error = ex.Message;
                        }                        
                    });
                }
            }
            catch (Exception ex)
            {
                TraceLog("Message iOS ShowAlert : " + ex.Message);
            }
        }

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

我从我的注册页面构造函数中调用它

Device.BeginInvokeOnMainThread(() =>
                {
                    CustomToast.LongMessage("Hiiiiii");
                });

它不会在任何地方被捕获,但它也不会显示。任何人都可以提出一些建议吗?

【问题讨论】:

    标签: xamarin.forms xamarin.ios uialertcontroller


    【解决方案1】:

    这是因为RegisterPage 是您的LoginPage 上显示的页面,UIApplication.SharedApplication.KeyWindow.RootViewController 此代码无法为RegisterPage 检索正确的视图控制器。它只是在前一页上显示了一个操作表,但是您的应用已经到达一个新页面,然后这个 Toast 可以显示在屏幕上。 首先,您必须找出窗口的首页:

    UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
    {
        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);
        }
        return rootViewController;
    }
    

    其次,调整您的演示代码,如:

    Device.BeginInvokeOnMainThread(() =>
    {
        try
        {
            alert = UIAlertController.Create("", messages, UIAlertControllerStyle.ActionSheet);
            topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController).PresentViewController(alert, true, null);
        }
        catch (Exception ex)
        {
            var Error = ex.Message;
        }
    });
    

    最后,您可以使用Navigation.PushModalAsync(new RegisterPage()); 来展示您的吐司

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多