分享在 IOS 上无法正常工作,或者显示弹出式分享非常慢 window

Share Not Working Properly on IOS or it's very slow to display the popup share window

我正在使用 Xamarin Forms 开发移动应用程序,我遇到了一个问题,该问题在 Android 上按预期工作,但在 IOS 上却没有,我有一个共享按钮调用传递标题和 uri 的工作平台的 built-in 共享,但是在 IOS 上,此功能似乎没有按预期工作,我不知道它是否很慢或有其他问题,所以你有多次点击共享图标以显示共享弹出窗口(同样这在 Android 上非常有效)。

请指教, 以下是代码:

<StackLayout Grid.Column="2" Grid.ColumnSpan="2" Spacing="0" Margin="0,5,13,0">
   <StackLayout.GestureRecognizers>
      <TapGestureRecognizer Tapped="ImageButton_Clicked"/>
   </StackLayout.GestureRecognizers>
   <Image Source="export" HeightRequest="27" WidthRequest="20" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,0,0"/>
</StackLayout>
private async void ImageButton_Clicked(object sender, System.EventArgs e)
{
       await Share.RequestAsync(new ShareTextRequest
       {
           Uri = "http://myurl",
           Title = "My Title"
       });
}

除了我之前的问题之外,我将分享整个 xaml 代码跃点,这可能会提供清晰的视图:

Xaml代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             xmlns:Helpers="clr-namespace:StandardsView.Helper"
             xmlns:telerikDataControls="clr-namespace:Telerik.XamarinForms.DataControls;assembly=Telerik.XamarinForms.DataControls"
             xmlns:telerikListView="clr-namespace:Telerik.XamarinForms.DataControls.ListView;assembly=Telerik.XamarinForms.DataControls"
             xmlns:local="clr-namespace:StandardsView.Views"
             xmlns:telerikPrimitives="clr-namespace:Telerik.XamarinForms.Primitives;assembly=Telerik.XamarinForms.Primitives"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="StandardsView.Views.PreviewStandard"
             x:Name="parent"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             ios:Page.UseSafeArea="False"
             NavigationPage.HasNavigationBar="False"
             BackgroundImageSource="{Binding AppModel.BackgroundImage}"
             IsEnabled="{Binding AppModel.IsBusy, Converter={Helpers:InverseBoolConverter}}">
    <Grid RowSpacing="0" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <telerikDataControls:RadListView x:Name="listView" Margin="10,0,0,0" SelectionMode="None" ItemsSource="{Binding AppModel.Benchmarks}"  HeaderTemplate="{StaticResource ListHeaderTemplate}" FooterTemplate="{StaticResource ListFooterTemplate}">
            <telerikDataControls:RadListView.ItemStyle>
                <telerikListView:ListViewItemStyle BackgroundColor="Transparent" BorderLocation="None" BorderWidth="0" BorderColor="Transparent" />
            </telerikDataControls:RadListView.ItemStyle>
            <telerikDataControls:RadListView.SelectedItemStyle>
                <telerikListView:ListViewItemStyle BackgroundColor="Transparent" />
            </telerikDataControls:RadListView.SelectedItemStyle>
            <telerikDataControls:RadListView.ItemTemplate>
                <DataTemplate>
                    <telerikListView:ListViewTemplateCell>
                        <telerikListView:ListViewTemplateCell.View>
                            <StackLayout Padding="0" Spacing="0" BackgroundColor="Transparent" Margin="0,0,0,10">
                                <telerikPrimitives:RadBorder CornerRadius="15, 15, 0, 0" Padding="0" Margin="0,0,12,0">
                                    <telerikPrimitives:RadBorder.Triggers>
                                        <DataTrigger TargetType="telerikPrimitives:RadBorder" Binding="{Binding ResourceCount}" Value="0">
                                            <Setter Property="CornerRadius" Value="15"/>
                                        </DataTrigger>
                                    </telerikPrimitives:RadBorder.Triggers>
                                    <Grid BackgroundColor="White">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="19"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="60"/>
                                            <ColumnDefinition Width="10"/>
                                        </Grid.ColumnDefinitions>
                                        <StackLayout Grid.Column="2" Grid.ColumnSpan="2" Spacing="0" Margin="0,5,13,0">
                                            <StackLayout.GestureRecognizers>
                                                <TapGestureRecognizer Command="{Binding ShareCommand}" />
                                            </StackLayout.GestureRecognizers>
                                            <Image Source="export" HeightRequest="27" WidthRequest="20" VerticalOptions="Center" HorizontalOptions="End" />
                                        </StackLayout>
                                        <StackLayout Grid.Column="1" Grid.ColumnSpan="2" Padding="0,0,0,10">
                                            <Grid Margin="0">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="130"/>
                                                    <ColumnDefinition Width="*"/>
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="Auto"/>
                                                </Grid.RowDefinitions>
                                                <BoxView BackgroundColor="{StaticResource CPALMS_BlueGreen}" CornerRadius="0,0,5,5" HeightRequest="22" WidthRequest="130"/>
                                                <Label Text="{Binding Code}" Style="{StaticResource BrowseItemTitle}" />
                                            </Grid>
                                            <Label MinimumHeightRequest="50" Style="{StaticResource BrowseItemDescription16}" Text="{Binding Description}" Margin="0,7,0,7" />
                                            <StackLayout>
                                                <StackLayout.Triggers>
                                                    <DataTrigger TargetType="StackLayout" Binding="{Binding Remarks}" Value="">
                                                        <Setter Property="IsVisible" Value="False"/>
                                                    </DataTrigger>
                                                </StackLayout.Triggers>
                                                <BoxView HeightRequest="1" BackgroundColor="{StaticResource CPALMS_BlueGreen}" ></BoxView>
                                                <Label Style="{StaticResource StdClarificationsTitleLabel}" Text="Clarifications" />
                                                <Label Style="{StaticResource StdClarificationsLabel}" Text="{Binding Remarks}" />
                                            </StackLayout>
                                            <BoxView HeightRequest="1" BackgroundColor="{StaticResource CPALMS_BlueGreen}" ></BoxView>
                                            <Label Style="{StaticResource StdInfoLabel}">
                                                <Label.Triggers>
                                                    <DataTrigger TargetType="Label" Binding="{Binding ContentComplexityDescription}" Value="">
                                                        <Setter Property="IsVisible" Value="False"/>
                                                    </DataTrigger>
                                                </Label.Triggers>
                                                <Label.FormattedText>
                                                    <FormattedString>
                                                        <Span Text="Content Complexity: " />
                                                        <Span Text="{Binding ContentComplexityDescription}"/>
                                                    </FormattedString>
                                                </Label.FormattedText>
                                            </Label>
                                            <Label Style="{StaticResource StdInfoLabel}">
                                                <Label.Triggers>
                                                    <DataTrigger TargetType="Label" Binding="{Binding DateStandard}" Value="">
                                                        <Setter Property="IsVisible" Value="False"/>
                                                    </DataTrigger>
                                                </Label.Triggers>
                                                <Label.FormattedText>
                                                    <FormattedString>
                                                        <Span Text="Date Adopted/Revised: " />
                                                        <Span Text="{Binding DateStandard}"/>
                                                    </FormattedString>
                                                </Label.FormattedText>
                                            </Label>

                                        </StackLayout>
                                    </Grid>
                                </telerikPrimitives:RadBorder>
                                <telerikPrimitives:RadBorder CornerRadius="0, 0, 15, 15" Padding="0" Margin="0,-2,12,0">
                                    <telerikPrimitives:RadBorder.Triggers>
                                        <DataTrigger TargetType="telerikPrimitives:RadBorder" Binding="{Binding ResourceCount}" Value="0">
                                            <Setter Property="IsVisible" Value="false"/>
                                        </DataTrigger>
                                    </telerikPrimitives:RadBorder.Triggers>
                                    <Grid BackgroundColor="#F29A33" Padding="0,5,0,5">
                                        <Grid.GestureRecognizers>
                                            <TapGestureRecognizer Tapped="PreviewStd" />
                                        </Grid.GestureRecognizers>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="19"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="40"/>
                                            <ColumnDefinition Width="7"/>
                                        </Grid.ColumnDefinitions>
                                        <StackLayout Grid.Column="1"  Padding="0,0,5,10">
                                            <StackLayout.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="PreviewStd"/>
                                            </StackLayout.GestureRecognizers>
                                            <Label Style="{StaticResource previewRelatedResourceLabel}">
                                                <Label.GestureRecognizers>
                                                    <TapGestureRecognizer Tapped="PreviewStd"/>
                                                </Label.GestureRecognizers>
                                                <Label.FormattedText>
                                                    <FormattedString>
                                                        <Span Text="{Binding ResourceCount}" FontSize="{StaticResource LargeHeaderSize}" />
                                                        <Span Text="{Binding RelatedResourcesLabel}" />
                                                    </FormattedString>
                                                </Label.FormattedText>
                                            </Label>
                                        </StackLayout>
                                        <StackLayout Grid.Column="2" Spacing="0" Padding="0,5,0,0">
                                            <StackLayout.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="PreviewStd"/>
                                            </StackLayout.GestureRecognizers>
                                            <Image Source="rightarrow" HeightRequest="31" WidthRequest="31" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,0,0"/>
                                        </StackLayout>
                                    </Grid>
                                </telerikPrimitives:RadBorder>
                            </StackLayout>
                        </telerikListView:ListViewTemplateCell.View>
                    </telerikListView:ListViewTemplateCell>
                </DataTemplate>
            </telerikDataControls:RadListView.ItemTemplate>
            <telerikDataControls:RadListView.LayoutDefinition>
                <telerikListView:ListViewLinearLayout  VerticalItemSpacing="4"/>
            </telerikDataControls:RadListView.LayoutDefinition>
        </telerikDataControls:RadListView>
        <local:NavigationBar Grid.Row="1" prism:ViewModelLocator.AutowirePartialView="{x:Reference parent}" VerticalOptions="End"/>
        <local:LoadingPanel  Grid.RowSpan="2" prism:ViewModelLocator.AutowirePartialView="{x:Reference parent}" IsVisible="{Binding AppModel.IsBusy}"/>
    </Grid>
</ContentPage>

型号Class:

using Prism.Commands;

namespace StandardsView.Models.Entities
{
    public class StandardViewModel
    {
        public virtual int ID { get; set; }
        public virtual string Description { get; set; }
        public virtual string Code { get; set; }
        public virtual string DateStandard { get; set; }
        public virtual string Status { get; set; }

        public virtual string SubjectAreaName { get; set; }
        public virtual string GradeLevelCode { get; set; }

        public virtual string DomainAlias { get; set; }
        public virtual string DomainDescription { get; set; }

        public virtual string IdeaAlias { get; set; }
        public virtual string IdeaTitle { get; set; }
        public virtual int ResourceCount { get; set; }

        public virtual string Remarks { get; set; }

        public virtual string RelatedResourcesLabel { get; set; }

        public virtual string ContentComplexityDescription { get; set; }

        public DelegateCommand ShareCommand { get; set; }

    }
}

视图模型:

using Prism.Commands;
using Prism.Navigation;
using System;
using Xamarin.Forms;
using Xamarin.Essentials;

namespace StandardsView.ViewModels
{
    public class PreviewStandardViewModel : ViewModelBase
    {
        public DelegateCommand PreviewCommand { get; }

        public PreviewStandardViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            App.GlobalAppModel.Benchmarks[0].ShareCommand = new DelegateCommand(() => ShareBenchmark());
            AppModel = App.GlobalAppModel;
            ShowBackButton = true;
            Instructions = "Benchmark details";
            PreviewCommand = new DelegateCommand(() => PreviewBenchmark());

        }

        private void PreviewBenchmark()
        {
            Launcher.OpenAsync(new Uri(string.Format(Constants.previewStandardUrl, App.GlobalAppModel.Benchmarks[0].ID)));
        }

        private void ShareBenchmark()
        {
            Device.BeginInvokeOnMainThread(async () =>
            {
                string strUri = string.Format(Constants.previewStandardUrl, App.GlobalAppModel.Benchmarks[0].ID);
                string strTitle = App.GlobalAppModel.Benchmarks[0].Code;
                string strText = App.GlobalAppModel.Benchmarks[0].Code + " - " + App.GlobalAppModel.Benchmarks[0].Description;
                await Share.RequestAsync(new ShareTextRequest
                {
                    Uri = strUri,
                    Title = strTitle,
                    Text = strText
                });
            });
        }
    }
}

还有一个重要的信息我必须提到,在上面的代码中,您将在模型 class 中看到一个在其中声明的 DelegateCommand,在 ViewModel class 中您将看到此委托已分配给该方法,这是我最后一次尝试,在此之前我使用的是:

<StackLayout Grid.Column="2" Grid.ColumnSpan="2" Spacing="0" Margin="0,5,13,0">
   <StackLayout.GestureRecognizers>
      <TapGestureRecognizer Tapped="ImageButton_Clicked"/>
   </StackLayout.GestureRecognizers>
   <Image Source="export" HeightRequest="27" WidthRequest="20" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,0,0"/>
</StackLayout>
private void ImageButton_Clicked(object sender, System.EventArgs e)
{
       Device.BeginInvokeOnMainThread(async () =>
            {
                string strUri = string.Format(Constants.previewStandardUrl, App.GlobalAppModel.Benchmarks[0].ID);
                string strTitle = App.GlobalAppModel.Benchmarks[0].Code;
                string strText = App.GlobalAppModel.Benchmarks[0].Code + " - " + App.GlobalAppModel.Benchmarks[0].Description;
                await Share.RequestAsync(new ShareTextRequest
                {
                    Uri = strUri,
                    Title = strTitle,
                    Text = strText
                });
            });
}

所以所有这些情况都会导致相同的行为,我现在分享了我的所有代码,希望这将有助于更好地理解我的问题。

1.Please 检查是否有任何其他视图与您的 StackLayout 重叠。与其他视图重叠的区域将不会响应 TapGestureRecognizer.

2.Run Main thread中的分享码:

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{

    Device.BeginInvokeOnMainThread(async () => {
        await Share.RequestAsync(new ShareTextRequest
        {
            Uri = "http://myurl",
            Title = "My Title"
        });
    });
}

我创建了一个新项目并在其中添加了您的代码,共享 window 在 iOS 方面运行良好。