【问题标题】:Xamarin Forms TapGestureRecognizer not working iOSXamarin Forms TapGestureRecognizer 不工作 iOS
【发布时间】:2022-01-15 19:27:20
【问题描述】:

我一直面临一个过去很多人都遇到过的问题,但似乎没有一个解决方案有效。

我有一个网格,里面有一个带有 TapGesture 的 StackLayout。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:local="clr-namespace:WodTracker.App.Components"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="WodTracker.App.Pages.Account.PinCodePage"
             BackgroundColor="White">
    <ContentPage.Content>
        <StackLayout VerticalOptions="FillAndExpand" BackgroundColor="White">
            <StackLayout.GestureRecognizers>
                <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="NumberCommand" CommandParameter="1"/>
            </StackLayout.GestureRecognizers>
            <Label HorizontalOptions="Center" VerticalOptions="EndAndExpand"  Text="1"  FontSize="Title" FontAttributes="Bold" TextColor="Black"></Label>
            <Label HorizontalOptions="Center" VerticalOptions="EndAndExpand" Text=" " FontSize="Small" TextColor="Black"></Label>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

应该命中的代码如下。没有在构造函数中初始化 just 方法。

private async void NumberCommand(object sender, EventArgs e) 
{
     var button = sender;
}

在 Android 上执行点击命令“NumberCommand”。但在 iOS 上它不会触发。 在这个website我发现有人提到

InputTransparent="True"

我尝试将其添加到父网格和 StackLayout 中,但没有成功。

希望有人能提供帮助。

我正在使用 Visual Studio 2022 和 Xamarin Forms 5.0.0.2244

已解决

'InputTransparent="True"' 存在问题,在基于 ToolmakerSteve 的 cmets 逐步重建页面后,我能够修复它。感谢大家的帮助。

【问题讨论】:

  • 首先,仅供参考:InputTransparent="True" 导致元素被输入忽略。您绝对不希望在元素上使用它。 (人们使用它的情况是,另一个元素“在”应该接收输入的元素“前面”。他们把它放在那个“前面”元素上,这样输入就可以“通过它”到达后面的元素。)谷歌xamarin forms inputtransparent.
  • 添加到您的问题页面的完整xaml。如果需要,您可以省略大部分细节,但需要查看页面上 ALL 元素的开头和结尾。类似于&lt;ContentPage&gt; &lt;Grid&gt; &lt;StackLayout&gt;&lt;/StackLayout&gt; &lt;someotherelement&gt;&lt;/someotherelement&gt; &lt;/Grid&gt; &lt;/ContentPage&gt;,但每个元素都在自己的行中。也许其他元素之一需要InputTransparent。虽然那时我预计它也会在 Android 上失败。
  • 如果页面上还有其他人,则还显示该 XAML 中的 所有手势识别器
  • 为解决方案/更新/Xamarin.Forms 5.0.0.2291 管理 Nugets。在 Mac 上,在您的项目中,找到 /bin 和 /obj 文件夹,删除它们 - 以 100% 确保一切都得到重建。考虑直接在 Mac 上测试,在 Mac 上使用更新的 VS。
  • @ToolmakerSteve 谢谢你的例子。/我用它从头开始,它可以工作。一定是一些不正确的设置。我仍然将 InputTransparent 某处设置为 True。

标签: xamarin.forms


【解决方案1】:

'InputTransparent="True"' 存在问题,在基于 ToolmakerSteve 的 cmets 逐步重建页面后,我能够修复它。感谢大家的帮助。

代码

<StackLayout VerticalOptions="FillAndExpand" BackgroundColor="White">
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="NumberCommand" CommandParameter="1"/>
                </StackLayout.GestureRecognizers>
                <Label HorizontalOptions="Center" VerticalOptions="EndAndExpand"  Text="1"  FontSize="Title" FontAttributes="Bold" TextColor="Black"></Label>
                <Label HorizontalOptions="Center" VerticalOptions="EndAndExpand" Text=" " FontSize="Small" TextColor="Black"></Label>
</StackLayout>

背后的代码

private async void NumberCommand(object sender, EventArgs e)
{
    var button1 = (TapGestureRecognizer)sender;
    string commandParameter = button1.CommandParameter.ToString();
}

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 2021-08-03
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2017-11-12
    • 2017-12-21
    • 2020-01-15
    • 1970-01-01
    相关资源
    最近更新 更多