【问题标题】:Xamarin - ImageButton doesnt activate every clickXamarin - ImageButton 不会激活每次点击
【发布时间】:2021-09-30 21:15:11
【问题描述】:

Screen

在上图中,您可以看到 ImageButton 有时会激活的位置。当我在蓝色区域点击垃圾邮件时,计数器有时会增加。我认为 ImageButton 顶部可能还有另一个图层,但我不知道如何修复它。下面是 XAML 代码。希望有人可以提供帮助。谢谢!

<StackLayout>
    <Label Text="Discover" TextColor="Black" FontSize="24" FontAttributes="Bold" Margin="15" />

    <CarouselView ItemsSource="{Binding plants}" HeightRequest="300" PeekAreaInsets="100">
        <CarouselView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Frame  HeightRequest="280" WidthRequest="180" BackgroundColor="Wheat" HasShadow="True" Margin="10" Padding="0" HorizontalOptions="CenterAndExpand" CornerRadius="10" >
                        <Grid>
                            <StackLayout>
                                <ImageButton Source="{Binding imgsource}" VerticalOptions="FillAndExpand" 
                                    Aspect="AspectFill" Opacity="0.8" Clicked="ImageButton_Clicked"/>
                            </StackLayout>

                            <StackLayout Margin="0,10" >

                                <Image Source="https://icons-for-free.com/iconfiles/png/512/bookmark-131964752402712733.png" HeightRequest="35"
                       Aspect="AspectFit" HorizontalOptions="EndAndExpand" Margin="5,-15"/>
                                <Label Text="{Binding name_norm}" TextColor="Black" FontSize="16" FontAttributes="Bold"
                       Margin="15,-10,0,0" VerticalOptions="EndAndExpand" />

                                <StackLayout Orientation="Horizontal" Margin="15,-8,0,0" >
                                    <Image Source="https://www.freeiconspng.com/thumbs/info-icon/info-icon-24.png" HeightRequest="15"
                       Aspect="AspectFit"/>
                                    <Label Text="{Binding name_lat}" TextColor="Black" FontSize="16" FontAttributes="Italic"  VerticalOptions="EndAndExpand" Margin="-5,0" />
                                </StackLayout>
                            </StackLayout>
                        </Grid>
                    </Frame>
                </StackLayout>
            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>
    <Label x:Name="label" Text="0 ImageButton clicks"
           FontSize="Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
</StackLayout>

这里是 C# 代码:

namespace PlantBase
{
public partial class MainPage : ContentPage
{
    int clickTotal;
    public MainPage()
    {
        InitializeComponent();
    }

    private void ImageButton_Clicked(object sender, EventArgs e)
    {
        clickTotal += 1;
        label.Text = $"{clickTotal} ImageButton click{(clickTotal == 1 ? "" : "s")}";
    }
}

}

【问题讨论】:

    标签: visual-studio xaml xamarin mobile imagebutton


    【解决方案1】:

    检查VisualElement.InputTransparent Property

    false 如果元素及其子元素应该接收输入; true 如果元素及其子元素都不应该接收输入,而是应该将输入传递给视觉上位于当前视觉元素后面的元素。 Default is false.

    您需要在文本 stackLayout 上将InputTransparent 设置为true

     <StackLayout  InputTransparent="True"  Margin="0,10" VerticalOptions="EndAndExpand" BackgroundColor="SaddleBrown">
         <Label Text="{Binding name_norm}" TextColor="White" FontSize="16" FontAttributes="Bold" Margin="15,-10,0,0" VerticalOptions="EndAndExpand" />
         <StackLayout Orientation="Horizontal" Margin="15,-8,0,0" BackgroundColor="Aqua">
             <Image Source="https://www.freeiconspng.com/thumbs/info-icon/info-icon-24.png" HeightRequest="15" Aspect="AspectFit" />
             <Label Text="{Binding name_lat}" TextColor="White" FontSize="16" FontAttributes="Italic"  VerticalOptions="EndAndExpand" Margin="-5,0" />
         </StackLayout>
     </StackLayout>
    

    【讨论】:

      【解决方案2】:

      好的,我找到了问题。就是在我把红旗放在顶部和底部的文本放在一个从上到下扩展的 Stacklayout 中之前。现在我将它们放在单独的 StackLayouts 中它可以工作并且 ImageButton 是免费的。

      之前/之后的照片。

      old StackLayout

      new Stacklayout

      新的 XAML 是:

      <CarouselView ItemsSource="{Binding plants}" HeightRequest="300" PeekAreaInsets="100">
              <CarouselView.ItemTemplate>
                  <DataTemplate>
                      <StackLayout>
                          <Frame  HeightRequest="280" WidthRequest="180" BackgroundColor="Wheat" HasShadow="True" Margin="10" Padding="0" HorizontalOptions="CenterAndExpand" CornerRadius="10" >
                              <Grid>
                                  <StackLayout>
                                      <ImageButton Source="{Binding imgsource}" VerticalOptions="FillAndExpand"
                                          Aspect="AspectFill" Opacity="0.9" Clicked="ImageButton_Clicked" />
                                  </StackLayout>
      
                                  <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="EndAndExpand" BackgroundColor="Aqua">
                                      <ImageButton Source="https://icons-for-free.com/iconfiles/png/512/bookmark-131964752402712733.png" HeightRequest="35"
                                                  Aspect="AspectFit" HorizontalOptions="EndAndExpand" Margin="5,0" BackgroundColor="Transparent" Clicked="ImageButton_Clicked_1" />
                                  </StackLayout>
                                  
                                  <StackLayout Margin="0,10" VerticalOptions="EndAndExpand" BackgroundColor="SaddleBrown">
      
                                      <Label Text="{Binding name_norm}" TextColor="White" FontSize="16" FontAttributes="Bold"
                                              Margin="15,-10,0,0" VerticalOptions="EndAndExpand" />
      
                                      <StackLayout Orientation="Horizontal" Margin="15,-8,0,0" BackgroundColor="Aqua" >
                                          <Image Source="https://www.freeiconspng.com/thumbs/info-icon/info-icon-24.png" HeightRequest="15"
                                              Aspect="AspectFit" />
                                          <Label Text="{Binding name_lat}" TextColor="White" FontSize="16" FontAttributes="Italic"  VerticalOptions="EndAndExpand" Margin="-5,0" />
                                      </StackLayout>
                                      
                                      
                                      
                                  </StackLayout>
                              </Grid>
                          </Frame>
                      </StackLayout>
                  </DataTemplate>
              </CarouselView.ItemTemplate>
      

      现在新问题。因为在底部有文本堆栈布局,文本所在的位置我无法按下 ImageButton。如何将 ImageButton 作为顶部“层”,这样我也可以在按下文本的同时按下它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-20
        • 2010-09-25
        • 2016-01-14
        • 2017-07-08
        • 1970-01-01
        • 1970-01-01
        • 2015-09-06
        • 1970-01-01
        相关资源
        最近更新 更多