【问题标题】:Xamarin.Forms iOS Frame TapGestureRecognizer only works on border of frameXamarin.Forms iOS Frame TapGestureRecognizer 仅适用于框架的边框
【发布时间】:2022-01-22 12:34:08
【问题描述】:

我在框架上有一个 TapGestureRecognizer,它在 Android 上运行良好,但在 iOS 上只能在框架的边框上工作。

我不知道为什么会这样,我试图将它更改为 Frame 内 Grid 的 Grid GR 但这并没有改变任何东西,Frame 仍然只会在边框上被点击。

不知道是不是因为和安卓的布局不一样。

这是我的代码

XAML

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="45"/>
        <RowDefinition x:Name="SecondRowDefinition"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <ContentView Content="{Binding Map}"
                 Grid.RowSpan="3"
                 VerticalOptions="FillAndExpand" />

    <Frame x:Name="SearchFrame"
           BackgroundColor="#0c0c0c"
           HasShadow="False">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
        <Image Source="icon_search_red_24" 
               VerticalOptions="Center" 
               HeightRequest="20" />
        <Label TextColor="White"
               FontSize="Small"
               Text="Search place by name"
               HorizontalTextAlignment="Start"
               Grid.Column="1" />
        </Grid>
        <Frame.GestureRecognizers>
            <TapGestureRecognizer
                NumberOfTapsRequired="1"
                Command="{Binding Source={RelativeSource AncestorType={x:Type vm:MapViewModel}}, Path=SearchBarTapped}"
                Tapped="Search_Tapped">
            </TapGestureRecognizer>
        </Frame.GestureRecognizers>
    </Frame>

后面的代码

public partial class MapPage : ContentPage
{
    private readonly MapViewModel _mapViewModel;

    public MapPage()
    {
        InitializeComponent();
        if (Device.RuntimePlatform == Device.iOS)
        {
            SecondRowDefinition.Height = 100;
            SearchFrame.HorizontalOptions = LayoutOptions.Fill;
            SearchFrame.Margin = new Thickness(15, 40, 15, -40);
            SearchFrame.Padding = new Thickness(10, 15, 96, 10);
            SearchFrame.CornerRadius = 25;
            CategoriesList.Margin = new Thickness(15, 5, 0, -10);
        }
        else
        {
            SecondRowDefinition.Height = 40;
            SearchFrame.HorizontalOptions = LayoutOptions.Start;
            SearchFrame.Margin = new Thickness(15, 11, 65, -6);
            SearchFrame.Padding = new Thickness(10, 10, 96, 10);
            SearchFrame.CornerRadius = 20;
            CategoriesList.Margin = new Thickness(15, 8, 0, 0);
        }
        BindingContext = _mapViewModel = new MapViewModel();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        _mapViewModel.OnAppearing();
    }

    private void Search_Tapped(object sender, EventArgs e)
    {
        if (Device.RuntimePlatform == Device.Android)
        {
            Vibration.Vibrate(TimeSpan.FromMilliseconds(10));
        }
    }
}

【问题讨论】:

    标签: c# ios xamarin.forms


    【解决方案1】:

    我未能重现此问题。为了查看 iOS 上的 Tap 是否存在一些固有问题,我简化了您的代码。

    (将下面代码中的所有“TestBugs”替换为您项目的命名空间)

    TapFramePage.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"
                 x:Class="TestBugs.TapFramePage">
        <ContentPage.Content>
            <StackLayout>
                <Frame x:Name="SearchFrame"
                   BackgroundColor="#0c0c0c"
                   HasShadow="False">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="30" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Label TextColor="White"
                       Text="Search place by name"
                       HorizontalTextAlignment="Start"
                       Grid.Column="1" />
                    </Grid>
                    <Frame.GestureRecognizers>
                        <TapGestureRecognizer
                        NumberOfTapsRequired="1"
                        Tapped="Search_Tapped">
                        </TapGestureRecognizer>
                    </Frame.GestureRecognizers>
                </Frame>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    TapFramePage.xaml.cs:

    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace TestBugs
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class TapFramePage : ContentPage
        {
            public TapFramePage()
            {
                InitializeComponent();
            }
    
            protected void Search_Tapped(object sender, EventArgs args)
            {
                // Do something here.
            }
        }
    }
    

    App.xaml.cs:

    ...
    public App()
    {
        InitializeComponent();
        MainPage = new TapFramePage();
    }
    ...
    
    

    在 Android 和 iOS 上都进行了测试。在两个平台上,点击标签(或标签左侧或右侧的空间),会遇到我在SearchTapped 上设置的断点。

    请试试这个版本,看看它在 iOS 上是否适合你。了解这对您是否有效将缩小问题范围。

    【讨论】:

    • 我通过改变页边距并把它变大一点解决了,谢谢你的回答
    猜你喜欢
    • 2015-09-20
    • 2010-10-22
    • 2011-05-05
    • 2019-11-15
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多