Button Click Event Not Firing

MatthewChanMatthewChan CAMember ✭✭
<AbsoluteLayout>
    <StackLayout x:Name="RedBorderArea" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0.5,0.5,1,1"> (Asumme there is another list of stuff in here) </StackLayout>
    <AbsoluteLayout x:Name="BlueSquare" LayoutFlags="PositionProportional, WidthProportional" LayoutBounds="1,0,1,1">
       <StackLayout>             
                   <Button Text="TestButton" FontSize ="11" HeightRequest="35"
                    IsVisible="True" AbsoluteLayout.LayoutFlags="PositionProportional,WidthProportional"
                    Clicked="OnClicked"/> <!-- This event is never firing! -->
     </StackLayout>  
    </AbsoluteLayout>
</AbsoluteLayout>

My TestButton event is never being fired. What is happening? My BlueSquare layout is overlapping the RedBorderArea and that seems to be causing an issue. What do I have to do so that my TestButton click event can fire? I also tried attaching the tapped gesture recognizer to the button but that event is not firing either.

Best Answer

Answers

  • WindyhenWindyhen USMember ✭✭

    Hi @MatthewChan
    Testing yours, with slight modif, works!

    .cs

    namespace TestClick
    {
        public partial class TestClickPage : ContentPage
        {
            public TestClickPage()
            {
                InitializeComponent();
            }
    
            async void OnClicked(object sender, EventArgs e)
            {
                await DisplayAlert("Test", "Clicked!!", "OK");
            }
        }
    }
    

    .xaml

    <?xml version="1.0" encoding="utf-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestClick" x:Class="TestClick.TestClickPage">
        <AbsoluteLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
            <StackLayout WidthRequest="300" HeightRequest="600" BackgroundColor="Red" x:Name="RedBorderArea" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0.5,0.5,1,1" />
            <AbsoluteLayout>
                <StackLayout BackgroundColor="Blue" x:Name="BlueSquare" AbsoluteLayout.LayoutFlags="PositionProportional, SizeProportional" AbsoluteLayout.LayoutBounds="1,0,1,1">
                    <Button Text="TestButton" FontSize="11" HeightRequest="35" IsVisible="True" Clicked="OnClicked" />
                    <!-- This event is never firing! -->
                </StackLayout>
            </AbsoluteLayout>
        </AbsoluteLayout>
    </ContentPage>
    
  • MatthewChanMatthewChan CAMember ✭✭
    edited July 2017

    Hi @Windyhen , can you try adding a few buttons with click events inside the first stack layout just to see if those events are swallowing the events inside the blue stack layout?

Sign In or Register to comment.