(iOS) Button Click doesn't get hit when adding Grid layout (xaml)

DigitalNutcaseDigitalNutcase CAMember ✭✭
edited February 2018 in Xamarin.Forms

I created simple blank Xamarin forms project. When I add the Grid layout object to my xaml, button clicks don't get hit. If I remove the grid, button clicks code is getting hit. Any ideas what is wrong?

Note: Fixed markdown in this discussion to correctly display the XAML.

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:BlankForm" x:Class="BlankForm.BlankFormPage">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="53"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <StackLayout Orientation="Vertical" Grid.Row="0" Grid.Column="0">
            <Button x:Name="Connection" Text="line" Clicked="Handle_Clicked"/>
            <Button x:Name="Btn2" Text="Btn2" Pressed="OnPress" Clicked="Handle_Clicked2" ></Button>
        </StackLayout>
    </Grid>
</ContentPage>

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited February 2018

    I see a lot of ampersand problems in the XAML.
    Please repost it using property markdown so the site parser doens't eat it.
    https://redpillxamarin.com/2016/12/13/faq-frequently-asked-questions/

    Or, if it really looks like this in your solution, that's a problem

  • DigitalNutcaseDigitalNutcase CAMember ✭✭

    Fixed the XAML in the posting to show the code correctly.

  • Hello @DigitalNutcase,
    Did you finally solve the problem?

  • DigitalNutcaseDigitalNutcase CAMember ✭✭

    Hi @SylvainMoingeon,
    I solved this issue by switching the Grid to StackLayout and that worked.
    Just for fun I just loaded my old project and updated to the latest Xamarin Forms and problem still exist!! Makes you think twice about using Xamarin Forms sometimes.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I'm sure I've used buttons inside grids. I have them all over the place in my apps.
    And I'm sure others have to. If this were a problem throughout Xamarin it would have caused havoc long ago.

  • NMackayNMackay GBInsider, University mod

    @DigitalNutcase said:
    Hi @SylvainMoingeon,
    I solved this issue by switching the Grid to StackLayout and that worked.
    Just for fun I just loaded my old project and updated to the latest Xamarin Forms and problem still exist!! Makes you think twice about using Xamarin Forms sometimes.

    9 times out of 10 I've refactored code to used Grid's instead of Stacklayout for better performance and behavior but occasionally buttons in grid's can be difficult in the past. I only use Stacklayout when the design requires it usually for centered and hope the layout engine calculate correctly.

  • DigitalNutcaseDigitalNutcase CAMember ✭✭

    @ClintStLaurent
    Buttons just in the Grid seem to work. It's when you wrap them around a StackLayout that they don't work. I took the above code from my Windows UWP XAML to recreate it in Xamarin Forms, so I can have some common code. Under UWP the buttons in the StackPanel work fine.

  • NMackayNMackay GBInsider, University mod

    @DigitalNutcase said:
    @ClintStLaurent
    Buttons just in the Grid seem to work. It's when you wrap them around a StackLayout that they don't work. I took the above code from my Windows UWP XAML to recreate it in Xamarin Forms, so I can have some common code. Under UWP the buttons in the StackPanel work fine.

    Stacklayot has an inbuilt padding you have to override (See Spacing property), that sticks 6 pixels/device layout between each stack element in the layout, as the buttons apply padding it can all go weird pretty quickly.

  • DigitalNutcaseDigitalNutcase CAMember ✭✭

    @NMackay said:

    @DigitalNutcase said:
    @ClintStLaurent
    Buttons just in the Grid seem to work. It's when you wrap them around a StackLayout that they don't work. I took the above code from my Windows UWP XAML to recreate it in Xamarin Forms, so I can have some common code. Under UWP the buttons in the StackPanel work fine.

    Stacklayot has an inbuilt padding you have to override (See Spacing property), that sticks 6 pixels/device layout between each stack element in the layout, as the buttons apply padding it can all go weird pretty quickly.

    @SylvainMoingeon
    Thanks NMackay, your suggestion helped. It now works. It's a problem with Grid row height for this row. It was set to 20. If I make it bigger to take in the size of the StackLayout, the buttons work. E.g. Change the first RowDefinition Height="20" to the value 80 (or "*"). And I'm using the latest Xamarin Forms (3.5.0....).

  • NMackayNMackay GBInsider, University mod

    @DigitalNutcase said:

    @NMackay said:

    @DigitalNutcase said:
    @ClintStLaurent
    Buttons just in the Grid seem to work. It's when you wrap them around a StackLayout that they don't work. I took the above code from my Windows UWP XAML to recreate it in Xamarin Forms, so I can have some common code. Under UWP the buttons in the StackPanel work fine.

    Stacklayot has an inbuilt padding you have to override (See Spacing property), that sticks 6 pixels/device layout between each stack element in the layout, as the buttons apply padding it can all go weird pretty quickly.

    @SylvainMoingeon
    Thanks NMackay, your suggestion helped. It now works. It's a problem with Grid row height for this row. It was set to 20. If I make it bigger to take in the size of the StackLayout, the buttons work. E.g. Change the first RowDefinition Height="20" to the value 80 (or "*"). And I'm using the latest Xamarin Forms (3.5.0....).

    Yeah, if there's no height available to render things get messy :)

Sign In or Register to comment.