HeightRequest is not honored if content is added below - xamarin

I am trying to lock the height of a header - or basically any kind of content on the top of a page. However, once scrollable content is added below the height changes. A simple example:
<ContentPage.Content>
<StackLayout Padding="0" Margin="0" Spacing="0">
<Frame Margin="0" HeightRequest="150" BackgroundColor="red" CornerRadius="0" HasShadow="False" Padding="0"></Frame>
</StackLayout>
</ContentPage.Content>
This works fine. However, adding a lot of simple label lines in a scrollview somehow changes the height of the frame.
<ContentPage.Content>
<StackLayout Padding="0" Margin="0" Spacing="0">
<Frame Margin="0" HeightRequest="150" BackgroundColor="red" CornerRadius="0" HasShadow="False" Padding="0"></Frame>
<ScrollView>
<StackLayout>
<Label Text="aaa" /> <!-- Repeated 54 times however removed here for simplicity-->
</StackLayout>
</ScrollView>
</StackLayout>
</ContentPage.Content>
The results end up like this:
Why does it happen and can I do anything to avoid it? And why is the height reduced to exactly that height? Thanks!

It doesn't seem like you're limiting the height of the ScrollView. So considering you're merely doing a HeightRequest, it may change as needed to fit everything.
It's usually safer to use Grids. In this example, something like this could help you:
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="150" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Frame Grid.Row="0" Margin="0" BackgroundColor="red" CornerRadius="0" HasShadow="False" Padding="0"></Frame>
<ScrollView Grid.Row="1">
<StackLayout>
<Label Text="aaa" /> <!-- Repeated 54 times however removed here for simplicity-->
</StackLayout>
</ScrollView>
</Grid>
</ContentPage.Content>

Related

Fixed Row and Column in a grid - Xamarin Forms

I am trying to build a grid, with the first row fixed during vertically scroll, and the first column fixed during horizontally scroll
Do you know any plugin/component to achieve this ?
Actually for this I use 3 scrollView :
One for the fixed row, with only horizontal scroll
One for the fixed column, with only vertical scroll
One for the other cell, with horizontal and vertical scroll.
Then I have to synchronize the scrollViews, to be sure the alignment is preserved.
This is really ugly to do, and it might be laggy with all this synchronizations...
<StackLayout Spacing="0">
<ScrollView
x:Name="ScrollEnteteSynthese"
Orientation="Horizontal"
Scrolled="ScrollEnteteSynthese_Scrolled"
>
<Grid
x:Name="gridEnteteSynthese"
BackgroundColor="{StaticResource Grey}"
ColumnSpacing="1"
RowSpacing="1"
/>
</ScrollView>
<StackLayout
Orientation="Horizontal"
Spacing="0"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
>
<ScrollView
x:Name="ScrollColSynthese"
Orientation="Vertical"
Scrolled="ScrollColSynthese_Scrolled"
HorizontalOptions="Fill"
VerticalOptions="FillAndExpand"
WidthRequest="100">
<Grid
x:Name="gridColSynthese"
ColumnSpacing="1"
RowSpacing="1"
BackgroundColor="{StaticResource Grey}"
/>
</ScrollView>
<ScrollView
x:Name="ScrollSynthese"
Orientation="Both"
Scrolled="ScrollSynthese_Scrolled"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
>
<Grid
x:Name="gridSynthese"
BackgroundColor="{StaticResource GreyLight}"
ColumnSpacing="1"
RowSpacing="1"
/>
</ScrollView>
</StackLayout>
</StackLayout>
Actually, there are many different layout can implement it. For example
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid HeightRequest="300" >
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="350" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.2*"/>
<ColumnDefinition Width="0.8*"/>
</Grid.ColumnDefinitions>
<Frame Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Padding="0">
<ScrollView BackgroundColor="LightBlue" VerticalScrollBarVisibility="Never" HorizontalScrollBarVisibility="Never">
<StackLayout Orientation="Horizontal">
//...
</StackLayout>
</ScrollView>
</Frame>
<Frame Grid.Row="1" Grid.Column="0" Padding="0" Margin="0,-5,0,0">
<ScrollView BackgroundColor="LightGreen" VerticalScrollBarVisibility="Never" HorizontalScrollBarVisibility="Never">
<StackLayout Orientation="Vertical">
//...
</StackLayout>
</ScrollView>
</Frame>
<Frame Grid.Row="1" Grid.Column="1" Padding="0" Margin="-5,-5,0,0">
<Grid Grid.Row="1" Grid.Column="1" BackgroundColor="LightPink">
//
</Grid>
</Frame>
</Grid>
</StackLayout>

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.

AutoScale Label Xamarin

I am working on an app that displays results from a RSS feed. I am trying to show the Title and the Date in the same row of a Stacklayout.
Xaml Code:
StackLayout>
<Label Text="60 Second Sports" FontAttributes="Bold" HorizontalOptions="Center"/>
<ListView x:Name="mainArticleRssList" IsPullToRefreshEnabled="True" Refreshing="ListItems_Refreshing" ItemTapped="RssList_ItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="15,0,15,0">
<Label Text="{Binding Title}"
LineBreakMode="WordWrap"
MaxLines="2"/>
<Label Text="{Binding PublishedDate}"
LineBreakMode="WordWrap"
MaxLines="1"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<controls:AdMobView />
</StackLayout>
Here is what it looks like:
What I Expect:
I want the Title to auto scale to fit in the same row as the date. Is it possible to set a width for the entire row and then have the two labels fit inside of the row?
Other fruits of a few hours of googling:
Here are some of the links I found, but some are old and not sure if they work.
This one is old Auto Scale Text Size
This one didn't work Auto-scaling label fontsize in Xamarin
I also found a couple Nuget packages, but i don't think i need to do that.
https://baskren.github.io/Forms9Patch/
https://forums.xamarin.com/discussion/43468/autosize-font-label
Use Grid Layout inside stack layout to achive it
<ListView x:Name="mainArticleRssList" IsPullToRefreshEnabled="True" HasUnevenRows="True" Refreshing="ListItems_Refreshing" ItemTapped="RssList_ItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="15,0,15,0">
<Grid x:Name="grd">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="7*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding Title}"
LineBreakMode="WordWrap"
MaxLines="2"/>
<Label Text="{Binding PublishedDate}" Grid.Column="1"
LineBreakMode="WordWrap"
MaxLines="1"/>
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Use this code and it will work for you.
Is that your needs like this screenshot?
You can use Grid to achieved that.
<ContentPage.Content>
<StackLayout>
<Label Text="Xamarin.Forms native cell" HorizontalTextAlignment="Center" />
<ListView x:Name="listView" CachingStrategy="RecycleElement"
ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Title}" Grid.Row="0" Grid.Column="0"
LineBreakMode="TailTruncation"
MaxLines="2" />
<Label Text="{Binding PublishedDate}" Grid.Row="0" Grid.Column="1"
LineBreakMode="WordWrap"
MaxLines="1"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
If title is too long, you can setLineBreakMode="TailTruncation" it will automatically truncate when the label reaches the end of the container.

Expanding ListView in Xamarin Forms

Hi I am creating an expanding ListView in Xamarin Forms. I have taken a grid with two rows. First height as 50.. and Second as AUTO... When I am clicking on first row and making the below row Visible. The below row becomes Visible but height of cell remains same.. I have made ListView rows as Uneven also.. Please find the code below
<localRenderer:NativeListView x:Name="SkillsListView"
HasUnevenRows="true"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,0,1,1"
Margin="5,5,5,5"
SelectionMode="None"
BackgroundColor="#F3F4F6"
SeparatorVisibility="None"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Padding="0">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<AbsoluteLayout HorizontalOptions="FillAndExpand"
VerticalOptions="Fill"
HeightRequest="50"
Grid.Row="0"
BackgroundColor="#F3F4F5">
<AbsoluteLayout Margin="5,5,5,5" AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,0,1,1"
BackgroundColor="#FCFCFD">
<Label Text="{Binding SkillCode}"
FontFamily="Roboto#300"
FontSize="16"
FontAttributes="None"
TextColor="#FF888888"
Margin="10,0,0,0"
AbsoluteLayout.LayoutBounds="0,0.5,-1,-1"
AbsoluteLayout.LayoutFlags="PositionProportional" />
<Image x:Name="statusButton"
Margin="0,0,20,0"
Source="{Binding StateIcon}" HeightRequest="7" WidthRequest="13" AbsoluteLayout.LayoutBounds="1,0.5,-1,-1" AbsoluteLayout.LayoutFlags="PositionProportional" />
<Button AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,0,1,1" BackgroundColor="Transparent" Clicked="SkillListHeaderTapped" CommandParameter="{Binding .}" />
</AbsoluteLayout>
</AbsoluteLayout>
<Editor Text="{Binding Description}"
FontFamily="Roboto#300"
FontSize="16"
IsEnabled="false"
IsVisible="{Binding IsVisible}"
Margin="10,0,0,10"
BackgroundColor="White"
FontAttributes="None"
Grid.Row="1"
TextColor="#FF686868"
/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</localRenderer:NativeListView>

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.

Resources