Xamarin.Forms 警告:尝试使用 iOS 图像/手势识别器在其视图不在窗口层次结构中的 * 上显示 * [英] Xamarin.Forms Warning: Attempt to present * on * whose view is not in the window hierarchy with iOS image/gesture recogniser

查看:13
本文介绍了Xamarin.Forms 警告:尝试使用 iOS 图像/手势识别器在其视图不在窗口层次结构中的 * 上显示 *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模态导航页面,其中的图像就像一个按钮;

I have a modal Navigation page with an image which acts like a button;

<Image Source ="share.png" HeightRequest="32" WidthRequest="32">
    <Image.GestureRecognizers>
        <TapGestureRecognizer Tapped="On_Share" />
    </Image.GestureRecognizers>
</Image>

以及后面的方法;

async void On_Share(object sender, EventArgs e)
{
    if (CrossConnectivity.Current.IsConnected)
    {
        var message = "Share this";
        var title = "Share";
        await CrossShare.Current.Share(new ShareMessage { Text = message, Title = title}, new ShareOptions { ExcludedUIActivityTypes = new[] { ShareUIActivityType.PostToFacebook } });
    }
    else
    {
        NoInternetLabel.IsVisible = true;
    }
 }

当我尝试单击共享图像/按钮时出现错误.我已将断点放入 On_Share 方法的第一行 &他们没有被击中.

I'm getting the error when I try to click on the share image/button. I've put breakpoints into the first line of the On_Share method & they're not being hit.

Warning: Attempt to present <UIActivityViewController: 0x141b60f70> on <Xamarin_Forms_Platform_iOS_ModalWrapper: 0x1419a0920> whose view is not in the window hierarchy!

请注意,这在 Android 中运行良好,我只在 iOS 中看到了问题.我不确定发生了什么 - 当我单击图像时,我没有尝试显示任何其他窗口或任何内容.无论如何,错误出现在进程到达 On_Share 方法的开头之前.我在这里错过了什么?

Please note this works fine in Android, I'm only seeing issues in iOS. I'm not sure what is going on - I'm not trying to present any other windows or anything when I click the image. Regardless, the error appears before the process reaches the beginning of the On_Share method. What am I missing here?

该方法现在确实被击中了,但我仍然收到错误消息.它一定是在尝试发送共享表但失败了...

The method does get hit now, and I'm still getting the error. It must be trying to send up the share sheet and failing...

推荐答案

最终 Share 插件出现了问题 - 我们通过递归部分代码来解决它.

There was a problem with the Share plugin in the end - we resolved it by making part of the code recursive.

GetVisibleViewController 过去看起来像这样;

the GetVisibleViewController used to look like this;

UIViewController GetVisibleViewController()
{
    var rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;

    if (rootController.PresentedViewController == null)
        return rootController;

    if (rootController.PresentedViewController is UINavigationController)
    {
        return ((UINavigationController)rootController.PresentedViewController).VisibleViewController;
    }

    if (rootController.PresentedViewController is UITabBarController)
    {
        return ((UITabBarController)rootController.PresentedViewController).SelectedViewController;
    }

    return rootController.PresentedViewController;
}

而它需要循环查找顶部的 UIViewController;

whereas it needed to cycle through to find the top UIViewController;

UIViewController GetVisibleViewController(UIViewController controller = null)
{
    controller = controller ?? UIApplication.SharedApplication.KeyWindow.RootViewController;

    if (controller.PresentedViewController == null)
        return controller;

    if (controller.PresentedViewController is UINavigationController)
    {
        return ((UINavigationController)controller.PresentedViewController).VisibleViewController;
    }

    if (controller.PresentedViewController is UITabBarController)
    {
        return ((UITabBarController)controller.PresentedViewController).SelectedViewController;
    }

    return GetVisibleViewController(controller.PresentedViewController);
}

我已提出问题并在 github

这篇关于Xamarin.Forms 警告:尝试使用 iOS 图像/手势识别器在其视图不在窗口层次结构中的 * 上显示 *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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