微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin-专注于入门且TapGestureRecognizer无法正常工作

如何解决Xamarin-专注于入门且TapGestureRecognizer无法正常工作

<StackLayout Orientation="Horizontal" FlowDirection="RightToLeft" HorizontalOptions="EndAndExpand" Spacing="0">
    <StackLayout.GestureRecognizers>
        <TapGestureRecognizer Tapped="GoToBasket"></TapGestureRecognizer>
    </StackLayout.GestureRecognizers>
    <Frame x:Name="frmBasket" CornerRadius="24" Padding="8"  BackgroundColor="#FFFFFF">
        <StackLayout Orientation="Horizontal">
            <Entry x:Name="lblTotalBasket" TextChanged="lblTotalBasket_TextChanged" HorizontalOptions="FillAndExpand" IsReadOnly="True" Text="{Binding TotalPrice,StringFormat='₺{0:0.00}'}" Margin="0,5,0" HorizontalTextAlignment="Center" FontSize="13"  VerticalTextAlignment="Center"  FontAttributes="Bold" BackgroundColor="#FFFFFF" TextColor="{StaticResource Primary}"></Entry>
            <Label x:Name="lblBasketIcon"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="Transparent" Text="&#xf291;" FontSize="15" TextColor="{StaticResource Primary}" Margin="8,0"  FontFamily="{StaticResource FontAwesomeSolid}"></Label>
        </StackLayout>
    </Frame>
</StackLayout>

StackLayout TapGestureRecognizer工作,但Entry元素除外。我尝试为Entry元素添加TapGestureRecognizer和Focused属性,但还是没有用。

StackLayout TapGestureRecognizer如何在输入元素点击上工作?

解决方法

如果将IsReadOnly=false设置为条目,则TapGestureRecognizer将不起作用,因为在点击时它将被聚焦,并且用户可以键入内容。因此,您可以在foucsed事件中调用方法:

<Entry x:Name="lblTotalBasket" Focused="lblTotalBasket_Focused"/>

在后面的代码中:

private void lblTotalBasket_Focused(object sender,FocusEventArgs e)
{
    Console.WriteLine("lblTotalBasket_Focused-entry");
}

如果将IsReadOnly=ture设置为条目,则TapGestureRecognizer将执行,也可以将TapGestureRecognizer直接添加到条目:

<Entry x:Name="lblTotalBasket" Focused="lblTotalBasket_Focused" HorizontalOptions="FillAndExpand" IsReadOnly="True" Text="{Binding TotalPrice,StringFormat='₺{0:0.00}'}" Margin="0,5,0" HorizontalTextAlignment="Center" FontSize="13"  VerticalTextAlignment="Center"  FontAttributes="Bold" BackgroundColor="#FFFFFF">
    <Entry.GestureRecognizers>
        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"></TapGestureRecognizer>
    </Entry.GestureRecognizers>
</Entry>

在后面的代码中:

private void TapGestureRecognizer_Tapped(object sender,EventArgs e)
{
    Console.WriteLine("TapGestureRecognizer_Tapped-entry");
}

更新:

在Android中,您还需要设置isEnable = false才能使TapGestureRecognizer工作:

<Entry x:Name="lblTotalBasket" IsEnabled="False"/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。