Xamarin Stacklayout VerticalOptions CenterAndExpand not working - xamarin

Hi Guys probably very easy question for Xamarin expert I am trying to vertically center align stacklayout control and it is working fine in android mobile but not working in windows mobile device. My code is below
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="aa.Views.MainPage">
<ScrollView>
<StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand"
Padding="20" Spacing="10">
<Entry x:Name="enEmail" Placeholder="Email"></Entry>
<Entry x:Name="enPassword" IsPassword="True" Placeholder="Password"></Entry>
<Button Text="Login" Clicked="OnClicked_btnLogin" x:Name="btnLogin"></Button>
<Button Text="Register" Clicked="OnClicked_btnRegister" x:Name="btnRegister"></Button>
</StackLayout>
</ScrollView>
</ContentPage>
For more details please have a look at the attached images and help me how would i align it in center of screen for windows phone

its StackLayout: puts views consecutive one another. If you want to move buttons to down side use Grid instead like:
<Grid RowSpacing="10"><!--RowSpacing gives some space between rows-->
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <!--You can also use constant size also-->
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/> <!--This fill the empty space-->
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Entry Grid.Row="0" Placeholder=" Email"/>
<Entry Grid.Row="1" Placeholder=" Password" IsPassword="True"/>
<Button Grid.Row="3" Text="Login" />
<Button Grid.Row="4" Text="Register" />
</Grid>

Try the code below, it will give you a scroll view plus your fields will be aligned to the center
<ScrollView>
<AbsoluteLayout>
<StackLayout Orientation="Vertical" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All" x:Name="maincontent" Spacing="0">
<StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand"
Padding="20" Spacing="10">
<Entry x:Name="enEmail" Placeholder="Email"></Entry>
<Entry x:Name="enPassword" IsPassword="True" Placeholder="Password"></Entry>
<Button Text="Login" Clicked="OnClicked_btnLogin" x:Name="btnLogin"></Button>
<Button Text="Register" Clicked="OnClicked_btnRegister" x:Name="btnRegister"></Button>
</StackLayout>
</StackLayout>
</AbsoluteLayout>
</ScrollView>

Related

Xamarin grid with buttons inside not clicking

Good afternoon all,
Having an interesting issue here where by I have 4 buttons within a grid that all work ... kind of ... the top two buttons work without an issue however the bottom two you can only click the top half of the button otherwise it does not register the action.
Below is the Xaml code I am using ... Hope this helps... so far everything I have read doesn't seem to work so I can only assume I am missing something very simple.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HomemasterMobileApplication.Views.HomePage"
Title=""
BackgroundImageSource = "HousingBackground2">
<ContentView >
<ScrollView>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="100"/>
<RowDefinition Height="15"/>
<RowDefinition Height="260"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="1" />
<StackLayout Grid.Row="1" Orientation="Horizontal" HorizontalOptions="Center" Padding="0,0,0,0" VerticalOptions="Center" Opacity="0.8" BackgroundColor="#002D485D" >
<ContentView>
<ImageButton Source="HomeMasterLogo.png" HeightRequest="100" CornerRadius="25"/>
</ContentView>
</StackLayout>
<StackLayout Grid.Row="3" Orientation="Horizontal" Padding="0,15,0,0" Spacing="5" HorizontalOptions="Center">
<Grid>
<Grid.ColumnDefinitions >
<!--0--><ColumnDefinition Width="10*"/>
<!--1--><ColumnDefinition Width="40*"/>
<!--2--><ColumnDefinition Width="10*"/>
<!--3--><ColumnDefinition Width="40*"/>
<!--4--><ColumnDefinition Width="10*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<!--0--><RowDefinition Height="10"/>
<!--1--><RowDefinition Height="125"/>
<!--2--><RowDefinition Height="40"/>
<!--3--><RowDefinition Height="125"/>
</Grid.RowDefinitions>
<Button ImageSource="homeuser24px"
BackgroundColor="#00AAA9"
BorderColor="White"
Opacity="1"
Text="Your Account"
TextTransform="Default"
BorderWidth="2"
CornerRadius="100"
TextColor="White"
FontSize="14"
Clicked="OnAccountClick"
Grid.Row="1" Grid.Column="1"
/>
<Button ImageSource="toolsadd24px"
BackgroundColor="#00AAA9"
BorderColor="White"
Opacity="1"
Text="Log a Repair"
TextTransform="Default"
BorderWidth="2"
CornerRadius="100"
TextColor="White"
FontSize="14"
Clicked="OnRepairRequestClick"
Grid.Row="1" Grid.Column="3"
/>
<Button ImageSource="toolsinfo24px"
BackgroundColor="#00AAA9"
BorderColor="White"
TextTransform="Default"
Opacity="1"
BorderWidth="2"
CornerRadius="100"
TextColor="White"
Text="Track a Repair"
FontSize="14"
Clicked="OnCheckRepairsClick"
Grid.Row="3" Grid.Column="1"
/>
<Button
Clicked="OnPersonalDetailsClick"
ImageSource="userinfo24px"
BackgroundColor="#00AAA9"
BorderColor="White"
TextTransform="Default"
Opacity="1"
BorderWidth="2"
CornerRadius="100"
TextColor="White"
Text="My Details"
FontSize="14"
Grid.Row="3" Grid.Column="3"
/>
</Grid>
</StackLayout>
</Grid>
</ScrollView>
</ContentView>
You need to use the DN function within the grid to adjust the grid height.
First, you define the grid on the outermost layer. The problem lies in Grid.Row="3".
Then you create another Grid in Grid.Row="3",But the height of Grid.Row="3" you define in the outer Grid is 260.Your internal view height exceeds it. Therefore, the view will be overwritten.
In general, you only need to increase the height of the external Grid.
<ScrollView>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="100"/>
<RowDefinition Height="15"/>
<RowDefinition Height="400"/>
<!--Change the bottom height to 400-->
</Grid.RowDefinitions>
....
</ScrollView>

How to fill the entire screen with two buttons vertically

I have to fill all the screen with two buttons vertically
I already tried to use fill and expand:
<ContentPage.Content>
<StackLayout>
<Button Text="Histórico"
BackgroundColor="Transparent"
BorderColor="Black"
BorderWidth="1"
Clicked="Timeline"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"/>
<Button Text="Minha Agenda"
BackgroundColor="Transparent"
BorderColor="Black"
BorderWidth="1"
Clicked="MinhaAgenda"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"/>
</StackLayout>
</ContentPage.Content>
It works, but this caused to much lag on the screen, i dont know why
Is there other solution for this?
How about using a Grid, something like:
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Text="History"
Grid.Row="0"
HorizontalOptions="Fill"
VerticalOptions="Fill" />
<Button Text="Agenda"
Grid.Row="1"
HorizontalOptions="Fill"
VerticalOptions="Fill" />
</Grid>
</ContentPage.Content>
This will fill the view with two equally sized buttons.

How can remove margin between items in xaml?

This is my xaml code:
<?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:TestSalesforce"
x:Class="TestSalesforce.MainPage">
<StackLayout Padding="0,0,0,0">
<!-- Place new controls here -->
<Label Text="LOGIN"
HorizontalOptions="Center" />
<Label Text="Email:" Margin="0,0,0,0"/>
<Entry x:Name="txtEmail" Margin="0,0,0,0"/>
<Label Text="Password:" Margin="0,0,0,0"/>
<Entry x:Name="txtPassword" Margin="0,0,0,0"/>
<Button x:Name="btnLogin" Text="Login"/>
<Button x:Name="btnClose" Text="Close" Clicked="OnClose"/>
</StackLayout>
</ContentPage>
I try using Margin="0,0,0,0" but it not ok:
This is result:
How can remove margin between items in xaml?
StackLayout and Grid have Spacing-Properties. Spacing describes the gap between each child element in the Layout.
I think by default a StackLayouts Spacing-Property is not 0 but something around 2-5.
Try setting Spacing="0" on the stacklayout.
Looks like you have styles that are already gettin applied to the elements. Possibly try settings padding on those elements to 0 (they may not support padding) or if that doesn't work you might need to add a negative margin to counteract the styles.
Try This Code: Using Grid format
<Grid Grid.Row="3">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Controls:Label Grid.Row="0" Font="15" Text="{Binding LabelAddress}"/>
<StackLayout>
<Controls:PlaceholderEditor Grid.Row="1" Text="{Binding TextAddress}" VerticalOptions="FillAndExpand" HorizontalOptions="Fill" Margin="0,20,0,0" Placeholder="Enter Address" PlaceholderColor="{StaticResource LightGray}" AutoSize="TextChanges"/>
</StackLayout>
</Grid>
The result like this:
Add Spacing=0 to your stack layout, so it looks the next:
<StackLayout Spacing=0 Padding="0,0,0,0">

Can not set the content of button as it doesn't have a contentpropertyattribute on xamarin page

I'm trying to add three button images on the same row inside a StackLayout.
The first button image doesn't show any error, but the problem is with two other buttons.
Here is the code:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1.5*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
<RowDefinition Height ="auto"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" HorizontalOptions="Center" VerticalOptions="Center">
<Image HorizontalOptions="Center" VerticalOptions="Center" Source="loginlogo.png"/>
</StackLayout>
<StackLayout Padding="5" Spacing="5" Grid.Row="1">
<StackLayout>
<Label Text ="editor is used for collecting text that is expected to take more than one line."
TextColor="Black" BackgroundColor="White" HeightRequest="100" />
</StackLayout>
<StackLayout HorizontalOptions="Center" VerticalOptions="Center"
Orientation="Horizontal">
<Button x:Name="facebook" WidthRequest="50" HeightRequest="50" Image="Fb.png">
<Button.Image Aspect="AspectFit" />
</Button>
<Button WidthRequest="50" HeightRequest="50" Image="LINKEDIN.png">
<Button.Image Aspect="AspectFit" />
</Button>
<Button x:Name="YB" WidthRequest="50" HeightRequest="50" Image="YOUTUBE.png">
<Button.Image Aspect="AspectFit" />
</Button>
</StackLayout>
</StackLayout>
<StackLayout Grid.Row="4" HorizontalOptions="Center" VerticalOptions="End" Orientation="Horizontal" Margin="1" >
<Label Text="sample text" TextColor="Gray" HorizontalOptions="Center" HorizontalTextAlignment="Center"/>
</StackLayout>
</Grid>
This is the exact error I receive:
Position 35:18. Can not set the content of Button as it doesn't have a
ContentPropertyAttribute
Position 35 is the third button named YB
I don't understand where the problem is.
Any advice will be helpful.
Thanks
Xamarin.Forms.Button.Image is an FileImageSource and thus there is no Aspect property to it. This abstract control is converted to a platform native one and full image control of how it appears within that button on each platform is not exposed.
But on Xamarin.Forms.Image there is an Aspect property.
If you need to control the aspect ratio, you could created a custom button renderer and handle it at the platform level, or just use a Xamarin.Forms.Image and set a tab gesture on them.

Entry height not working on xamarin forms

I want to set the height of the Entries showed on the next peace of code of a Xamarin.Forms project but it just doesn't work..
If I set the height of the row to something like: Height="1.3*", the Entry takes the whole height of the row but the placeholder won't be vertical aligned so I would like to use HieghtRequest instead.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage BackgroundColor="White">
<Grid BackgroundColor="Transparent" RowSpacing="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="4*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" VerticalOptions="FillAndExpand" HorizontalOptions="CenterAndExpand" Padding="0, 30, 0 ,0" RowSpacing="15">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Entry Grid.Row="0" Placeholder="{Binding emailAddress}" VerticalOptions="Center" HeightRequest="70" WidthRequest="330" BackgroundColor="Gray" TextColor="Black" HorizontalOptions="CenterAndExpand"/>
<Entry Grid.Row="1" Placeholder="{Binding password}" HeightRequest="300" VerticalOptions="Center" WidthRequest="330" IsPassword="true" BackgroundColor="Gray" TextColor="Black" HorizontalOptions="CenterAndExpand"/>
<StackLayout Grid.Row="2">
</StackLayout>
<Button Grid.Row="4" BackgroundColor="Red" TextColor="White" HorizontalOptions="CenterAndExpand" Text="{Binding signIn}" HeightRequest="70" WidthRequest="330"/>
</Grid>
<Grid Grid.Row="1">
</Grid>
</Grid>
Entry Height is not working because you have given 1* as RowDefinition. You can set RowDefinition as Auto and set HeightRequest againt each controls as prefered.
Also Use YAlign="Center" to vertically align the text. I hope this will help

Resources