【问题标题】:Xamarin forms, buttons inside frame are not firing an eventXamarin 表单,框架内的按钮未触发事件
【发布时间】:2021-08-16 10:46:54
【问题描述】:

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

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

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

表单来自此页面的示例 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> 

你能帮帮我吗? 谢谢

【问题讨论】:

  • 是您链接到的示例,与覆盖它们的按钮在同一行中有一个标签,因此它们不会接收任何输入。
  • 去掉Frame上的样式,重新测试一下。

标签: xamarin xamarin.forms


【解决方案1】:

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

github上有报道https://github.com/xamarin/Xamarin.Forms/issues/6760

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

【讨论】:

  • 提醒一下,MAUI中不推荐这两个,最新dev blog.
猜你喜欢
  • 1970-01-01
  • 2019-06-06
  • 2016-05-23
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 2010-10-21
  • 2013-01-17
  • 2012-03-14
相关资源
最近更新 更多