【问题标题】:Xamarin.Essentials Share.RequestAsync() dont working on iOSXamarin.Essentials Share.RequestAsync() 在 iOS 上不起作用
【发布时间】:2019-12-12 21:08:19
【问题描述】:

我在尝试与 iOS 上的 xamarin.Essentials 共享一些 pdf 文件时遇到了一些麻烦,该方法在 Android 中按预期工作,但是当我在 iOS 中运行时,调用该方法时没有错误但没有任何反应,共享菜单没有打开并且页面仍然运行没有错误,我将在下面发布一些代码:

PCL View.xaml(共享按钮所在的位置)

<ContentPage  xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             BackgroundColor="#6f7d8b" 
             x:Class="SeusPPPs.PDFViewer.View"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             ios:Page.UseSafeArea="true"
             xmlns:controls="clr-namespace:SeusPPPs">
    <ContentPage.Content>
        <Grid  RowSpacing="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="5*"/>
                <RowDefinition Height="95*"/>
            </Grid.RowDefinitions>
            <Label Padding="10,10,10,10" Grid.Row="0" VerticalOptions="Center" Text="COMPARTILHAR" TextColor="White" FontAttributes="Bold" HorizontalOptions="StartAndExpand">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="SHARE" NumberOfTapsRequired="1"/>
                </Label.GestureRecognizers>
            </Label>
            <Label Padding="10,10,10,10" Grid.Row="0" VerticalOptions="Center" Text="FECHAR" TextColor="White" FontAttributes="Bold" HorizontalOptions="EndAndExpand">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="Close" NumberOfTapsRequired="1"/>
                </Label.GestureRecognizers>
            </Label>

            <controls:PdfWebView
            x:Name="PdfView" 
            Grid.Row="1"
                             HorizontalOptions="FillAndExpand"
                             VerticalOptions="FillAndExpand" />
        </Grid>
    </ContentPage.Content>

</ContentPage>

代码隐藏分享事件

public void SHARE(object sender,EventArgs e)
        {
            if (_filepath == null)
                return;
            try
            {
                Device.BeginInvokeOnMainThread(async () => {
                    await Share.RequestAsync(new ShareFileRequest() {
                         Title = "Compartilhar seu PPP",
                         File = new ShareFile(_filepath)
                    });

                });

            }catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }


        }

加载 PDF 方法,在 ios 和 android 上都可以正常工作

private void CarregarPdf(string id)
        {

            var dependency = DependencyService.Get<ILocalFileProvider>();

            if (dependency == null)
            {
                DisplayAlert("Erro ao carregar dependencia", "Dependencia não encontrada", "OK");

                return;
            }

            var localPath = string.Empty;

            string url = "apiurl" + id;

            var fileName = Guid.NewGuid().ToString();

            using (var httpClient = new HttpClient())
            {
                var pdfStream = Task.Run(() => httpClient.GetStreamAsync(url)).Result;
                localPath =
                    Task.Run(() => dependency.SaveFileToDisk(pdfStream, $"{fileName}.pdf")).Result;
            }

            if (string.IsNullOrWhiteSpace(localPath))
            {
                DisplayAlert("Error baixar PDF", "não foi possivel encontrar o arquivo", "OK");

                return;
            }
            _filepath = localPath;
            PdfView.Uri = localPath;
        }

//编辑

我确实注意到问题仅在物理设备上,您可以使用此存储库 Github 重现错误

【问题讨论】:

  • 你是否在SHARE方法中添加了breakpoint,并在点击分享按钮时检查Share.RequestAsync是否被执行。
  • 从哪里调用CarregarPdf(string id) 方法?你可以设置断点并检查是否在点击标签后调用?
  • @JackHua-MSFT 是的,每次点击都会调用它
  • 你能在 Github 上创建一个示例项目,我们可以下载并在我们的机器上试用吗?
  • @Saamer 我将创建一个示例项目

标签: c# xamarin.forms xamarin.ios


【解决方案1】:

升级到 Xamarin 4.8 后,问题依旧存在。但是当我添加一个虚拟的 PresentationSourceBounds 时,它似乎已经解决了这个问题?
注意:我没有在早期版本的 Xamarin 上尝试过。

await Share.RequestAsync(new ShareFileRequest
{
  Title = result.Data.Name,
  File = new ShareFile(temporaryFile),
  PresentationSourceBounds = new Rectangle(0, 0, 1, 1) // Fixes iPad share option
});

【讨论】:

  • 在 2021 年仍然是一个问题,但解决方案有效。
猜你喜欢
  • 2016-07-03
  • 2012-05-29
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2015-02-02
相关资源
最近更新 更多