xamarin.ios - Xamarin.Forms 警告 : Attempt to present * on * whose view is not in the window hierarchy with iOS image/gesture recogniser

标签 xamarin.ios xamarin.forms share uitapgesturerecognizer

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

<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 方法的第一行并且它们没有被命中。

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 方法的开头之前。我在这里缺少什么?

编辑:该方法现在确实受到影响,但我仍然遇到错误。它一定是在尝试发送共享表但失败了...

最佳答案

Share 插件最终出现了问题 - 我们通过递归部分代码解决了这个问题。

GetVisibleViewController 以前是这样的;

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;

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.ios - Xamarin.Forms 警告 : Attempt to present * on * whose view is not in the window hierarchy with iOS image/gesture recogniser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41241508/

相关文章:

xamarin - 后退按钮按下事件Xamarin.Forms上的确认对话框

xamarin - 有人能够通过 Xamarin Forms 和 AppCenter.ms 看到崩溃附件吗?

android - 如何将 "copy to clipboard"添加到自定义 Intent Chooser?

azure - 带有 Azure 通知中心的 Xamarin Forms IOS

ios - 在 xamarin.ios 中使用带有 nativecss 组件的模糊过滤器

ios - 带有 UITextField : Trouble with Delegates and Dequequing 的自定义 Xib UITableViewCell

firebase - 为什么我的 Xamarin.iOS 应用程序没有收到来自 Google Firebase 的推送通知?

xaml - Xamarin Forms - Listview viewcell 使用 xaml 向左和向右滑动事件

types - 在 C# 中的多个 wsdl 引用之间共享类型

c# - 从电子邮件中将照片分享到 Facebook