首页 > 解决方案 > Xamarin 表单,框架内的按钮未触发事件

问题描述

我有一个表格,里面有一个框架和 3 个按钮。

click 事件不会触发到这些按钮中的任何一个。

我在框架外放了一个按钮,它可以点击。

该表单来自此页面 https://askxammy.com/replicating-user-profile-ui-in-xamarin-forms/的示例。

我问过作者,她没有回答。这是代码。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="UserProfileUISample.MainPage">

    <Grid BackgroundColor="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" /> 
        </Grid.RowDefinitions>

     <!-- 1. Profile picture-->   
     <Image Grid.Row="0" Source="KattyWhite" VerticalOptions="Start" /> 

     <!-- 2. Contact informaqtion frame-->   
     <StackLayout Grid.Row="0" BackgroundColor="White" VerticalOptions="End">
        <Frame CornerRadius="40" Style="{StaticResource stlMainFrame}" >
            <!-- Blocks: 3 and 4 -->
            <Grid Padding="25,10,25,0" RowSpacing="0"> 
               <Grid.ColumnDefinitions>
                   <ColumnDefinition Width="*"/>
                   <ColumnDefinition Width="*"/>
                   <ColumnDefinition Width="*"/>
               </Grid.ColumnDefinitions> 
               <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/> 
               </Grid.RowDefinitions>
                  <!-- 3. Phone & message buttons-->
                <Button x:Name="btn1" Grid.Row="0" Grid.Column="1" Style="{StaticResource stlContactBtn}" HorizontalOptions="End"   ImageSource="Phone" Clicked="OnClicked"/>
                 <Button x:Name="btn2" Grid.Row="0" Grid.Column="2" Style="{StaticResource stlContactBtn}" HorizontalOptions="Start" ImageSource="Correo" Clicked="Button_Clicked_1"/>
                 <!-- 4. Contact information-->
                 <Label x:Name="lblName" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Text="Katty White" FontAttributes="Bold" FontSize="20"/>
                 <Label x:Name="lblCode" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Text="New York" TextColor="#a5a2a2" FontSize="16"/>
                 <!--<Label Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Padding="0,10,0,0" FontSize="16">
                   <Label.FormattedText>
                        <FormattedString>
                            <Span Text="Email: " FontAttributes="Bold" />
                            <Span Text=" kattywhite@hotmail.com"  /> 
                        </FormattedString>
                    </Label.FormattedText>
                 </Label>-->
                <!--4. Contact information: Board inforation-->
                <Label Grid.Row="3" Grid.Column="0" Text="Λαχεία" Style="{StaticResource stlBoardTitle}" />
                <Label x:Name="lblLaxeia" Grid.Row="4" Grid.Column="0" Text="678" Style="{StaticResource stlBoardDesc}"/>

                <Label Grid.Row="3" Grid.Column="1" Text="Γεμίσματα" Style="{StaticResource stlBoardTitle}"/>
                <Label x:Name="lblGemismata" Grid.Row="4" Grid.Column="1" Text="340" Style="{StaticResource stlBoardDesc}"/>

                <Label Grid.Row="3" Grid.Column="2" Text="Κληρώσεις" Style="{StaticResource stlBoardTitle}"/>
                <Label x:Name="lblKliroseis" Grid.Row="4" Grid.Column="2" Text="67k" Style="{StaticResource stlBoardDesc}"/>
                <!--4. Contact information: Follow button-->
                <Button x:Name="btn3" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" TextColor="White" BackgroundColor="#2193f3" Text="Αναλυτικά" Margin="0,20,0,2" FontAttributes="Bold" FontSize="17" HeightRequest="45"/>
            </Grid> 
         </Frame>
        <Button x:Name="btn22" Grid.Row="6" Grid.Column="2" Style="{StaticResource stlContactBtn}" HorizontalOptions="Start" ImageSource="Correo" Clicked="Button_Clicked_1"/>

    </StackLayout>
   
</Grid> 

你能帮助我吗?谢谢

标签: xamarinxamarin.forms

解决方案


当您将TranslationY="-50"属性设置为按钮时,它将使按钮超出 的范围Grid,因此它不会响应单击事件。

github上有一个报告https://github.com/xamarin/Xamarin.Forms/issues/6760

我建议您将这些按钮或标签直接添加到 Grid 并使用absolute-layoutrelative-layout或其他布局来固定它们的位置。


推荐阅读