【问题标题】:Xamarin - Entry Focused and TapGestureRecognizer not workingXamarin - 进入重点和 TapGestureRecognizer 不工作
【发布时间】:2020-10-08 13:41:15
【问题描述】:
<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, 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, 0, 0"  FontFamily="{StaticResource FontAwesomeSolid}"></Label>
        </StackLayout>
    </Frame>
</StackLayout>

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

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

【问题讨论】:

    标签: xamarin xamarin.android


    【解决方案1】:

    如果您将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为entry,TapGestureRecognizer会执行,你也可以直接将TapGestureRecognizer添加到entry中:

    <Entry x:Name="lblTotalBasket" Focused="lblTotalBasket_Focused" HorizontalOptions="FillAndExpand" IsReadOnly="True" Text="{Binding TotalPrice, StringFormat='₺{0:0.00}'}" Margin="0, 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"/>
    

    【讨论】:

    • isEnabled = "False" 为我工作,感谢您的帮助
    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2022-11-29
    相关资源
    最近更新 更多