共享在IOS上无法正常工作,或者显示弹出共享窗口非常慢

时间:2019-12-03 14:06:08

标签: xamarin.forms xamarin.ios xamarin.essentials

我正在使用Xamarin Forms开发移动应用程序,遇到了一个问题,该问题在Android上可以正常工作,但在IOS上却没有,我有一个共享按钮,该按钮将调用工作平台的内置共享传递标题和uri,但是在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>

模型类:

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; }

    }
}

ViewModel:

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
                });
            });
        }
    }
}

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

<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 个答案:

答案 0 :(得分:1)

1。请检查您的StackLayout上是否有其他视图重叠。与其他视图重叠的区域将不响应TapGestureRecognizer

2。在Main thread中运行共享代码:

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{

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

我创建了一个新项目,并在其中添加了代码,共享窗口在iOS端运行良好。