简体   繁体   English

Xamarin.Forms iOS Frame TapGestureRecognizer 仅适用于边框的边框

[英]Xamarin.Forms iOS Frame TapGestureRecognizer only works on border of frame

I have a TapGestureRecognizer on a Frame that works perfectly well on Android but only works on the border of the frame on iOS.我在框架上有一个 TapGestureRecognizer,它在 Android 上运行良好,但仅在 iOS 上的框架边框上工作。

I have no idea why it does that, I tried to change it to a Grid GR for the Grid inside of the Frame but that doesn't change anything, the Frame still only gets clicked on the border.我不知道为什么会这样,我试图将它更改为 Frame 内部 Grid 的 Grid GR 但这并没有改变任何东西,Frame 仍然只会在边框上被点击。

I don't know if it's because there's a different layout than Android.不知道是不是因为和Android的布局不同。

Here's my code这是我的代码

XAML 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>

Code behind背后的代码

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

I failed to reproduce this problem.我未能重现此问题。 To see whether there was some inherent issue with Tap on iOS, I simplified your code.为了查看在 iOS 上的 Tap 是否存在一些固有问题,我简化了您的代码。

(replace all "TestBugs" in code below, with your project's namespace) (将下面代码中的所有“TestBugs”替换为您项目的命名空间)

TapFramePage.xaml: 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: 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: App.xaml.cs:

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

Tested this on both Android and iOS.在 Android 和 iOS 上对此进行了测试。 On both platforms, tapping on the label (or on the space to the left or right of it), hits a breakpoint I set on SearchTapped .在这两个平台上,点击 label(或在其左侧或右侧的空间),会遇到我在SearchTapped上设置的断点。

Please try this version, and see if it works for you on iOS.请尝试这个版本,看看它是否适用于 iOS。 Knowing whether this does or does not work for you will narrow down the problem.知道这对您是否有效将缩小问题范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM