Home > Mobile >  Why does my ImageButton not work in AbsoluteLayout? In .NET MAUI
Why does my ImageButton not work in AbsoluteLayout? In .NET MAUI

Time:12-28

I have an ImageButton in a Grid. Around the ImageButton, I have an <AbsoluteLayout>. When clicking, nothing happens or the method is not executed.

My XAML:

<VerticalStackLayout
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand"
    BackgroundColor="#24D4A3">
    <Grid BackgroundColor="White">
        <AbsoluteLayout>
            <ImageButton 
                                Clicked="CreateNote"                     
                                Source="add.png"
                                BorderColor="#2b3c3c" 
                                BorderWidth="0" 
                                BackgroundColor="#34A4EB" 
                                CornerRadius="35" 
                                WidthRequest="70" 
                                HeightRequest="70" 
                                AbsoluteLayout.LayoutBounds="313,625" 
                                Padding="2,0,0,0"/>
        </AbsoluteLayout>
    </Grid>
</VerticalStackLayout>

The method CreateNote:

private async void CreateNote(object sender, EventArgs e)
{
    await Shell.Current.GoToAsync("//CreateNote");
}

I would be grateful for any help!!

CodePudding user response:

I copied your code to my app and did a test,I found the space above your ImageButton is so large that I couldn't see it on my emulator.

So I added a ScrollView out of the VerticalStackLayout.Then I could see the ImageButton after I scrolled my page. And I could also trigger the clicked event ImageButton_Clicked of ImageButton .

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1226.MainPage">

    <ScrollView>
        <VerticalStackLayout
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand"
    BackgroundColor="#24D4A3">
            <Grid BackgroundColor="White">
                <AbsoluteLayout>
                    <ImageButton 
                                Clicked="ImageButton_Clicked"                     
                                Source="icon.png"
                                BorderColor="#2b3c3c" 
                                BorderWidth="0" 
                                BackgroundColor="#34A4EB" 
                                CornerRadius="35" 
                                WidthRequest="70" 
                                HeightRequest="70" 
                                AbsoluteLayout.LayoutBounds="313,625" 
                                Padding="2,0,0,0"/>
                </AbsoluteLayout>
            </Grid>

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

Function ImageButton_Clicked

private void ImageButton_Clicked(object sender, EventArgs e) 
      {
            System.Diagnostics.Debug.WriteLine(" click this ImageButton ");
      }
  • Related