Xamarin Grid Height changes after Images Loaded - xamarin

As i shows in attached images, my grid row is square shaped space before connecting to server and load the images, but when images are loaded it changes it's height and become rectangle shape. how can i avoid it ? i want to keep the square shaped space.
my photos are also squared.
before and after after connecting to server
[1]: https://i.stack.imgur.com/lZj6F.png
[2]: https://i.stack.imgur.com/ZaKQd.png
Code :
<Grid.RowDefinitions>
</Grid.RowDefinitions>
<Image x:Name="imgStation1" Grid.Row="0" Grid.Column="0" BackgroundColor="White" Source="saturday.jpg" Aspect="AspectFill" />
<StackLayout Grid.Row="0" Grid.Column="0" VerticalOptions="StartAndExpand" Spacing="10" Margin="10">
<Label x:Name="lblLiveStation1" Text="" FontSize="Small" TextColor="AntiqueWhite">
<Label.FontFamily>
<OnPlatform
x:TypeArguments="x:String"
Android="gothic.ttf#gothic"
iOS="Optima-Regular" />
</Label.FontFamily>
</Label>
<Label x:Name="lblDescriptionStation1" Text="Loading..." FontSize="Medium" FontAttributes="Bold" TextColor="FloralWhite">
<Label.FontFamily>
<OnPlatform
x:TypeArguments="x:String"
Android="gothic.ttf#gothic"
iOS="Optima-Regular" />
</Label.FontFamily>
</Label>
<Label x:Name="lblTimeStation1" Text="" FontSize="Small" FontAttributes="Bold" TextColor="GhostWhite">
<Label.FontFamily>
<OnPlatform
x:TypeArguments="x:String"
Android="gothic.ttf#gothic"
iOS="Optima-Regular" />
</Label.FontFamily>
</Label>
<ImageButton x:Name="imgPlayPause0" Grid.Row="0" VerticalOptions="End" HorizontalOptions="Start" Source="playlogo.png" BackgroundColor="Transparent" Clicked="imgPlayPause0_Clicked"></ImageButton>
</StackLayout>
</StackLayout>

Have you tried giving Aspect="Fill" ?
If you want to have image with height = width(i.e., Square), then give hardcoded width or height to it.
If that is not possible in your case, then try using RelativeSource where WidthHeight ="*" and height will be relative to Width like this WidthRequest="{Binding Source={x:Reference Self}, Path=WidthRequest}}"

Related

How to make Grid Row dynamic in CollectionView control of .Net MAUI?

I'm trying to achieve dynamic row height in CollectionView control, so that when ever I have more text for a particular property it will extend the height of the frame automatically.
I have tried with ListView as well using HasUnEvenRow property "true" but with that also it's not working.
Here is my code:
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid RowDefinitions="*,60" BackgroundColor="{StaticResource PageBackgroundColor}" Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<CollectionView Grid.Row="0" HorizontalOptions="Fill" VerticalOptions="Fill"
ItemsSource="{Binding Inspections}"
VerticalScrollBarVisibility="Always"
ItemsLayout="VerticalList" ItemSizingStrategy="MeasureAllItems">
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame Padding="15" HasShadow="False">
<Grid HorizontalOptions="Fill"
VerticalOptions="Fill"
BackgroundColor="White"
RowSpacing="25"
RowDefinitions="Auto,Auto,Auto"
ColumnDefinitions="*,Auto">
<StackLayout
Orientation="Horizontal"
Grid.Row="0"
Grid.Column="0">
<Label Text="{Binding Path=BusinessName}"
Style="{StaticResource LabelTitleStyle}" />
<Grid Padding="15,0,0,0">
<baseChip:Chip
HorizontalOptions="Fill" VerticalOptions="Fill"
Style="{StaticResource ChipContainer}"
HasShadow="False"
BackgroundColor="{Binding Path=ChipBackgroundColor}">
</baseChip:Chip>
<Label
Text="{Binding Path=ChipText}"
Style="{StaticResource ChipLabel}"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
TextColor="{Binding Path=ChipTextColor}"/>
</Grid>
</StackLayout>
<Image
Grid.Row="0"
Grid.Column="1"
HorizontalOptions="End"
VerticalOptions="Center"
Aspect="AspectFit">
<Image.Source>
<FontImageSource Glyph="{x:Static helper:MaterialFontHelper.FilePdfBox}"
Color="{StaticResource DarkGray}"
Size="20"
FontFamily="MaterialDesignIcons"/>
</Image.Source>
</Image>
<Grid Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
RowDefinitions="Auto, Auto"
ColumnDefinitions="*, *">
<Label
Grid.Row="0"
Grid.Column="0"
Text="Inspection Type"
Style="{StaticResource LabelKeyStyle}" />
<Label
Grid.Row="1"
Grid.Column="0"
Text="ksd kahdkahd kahd kahd aojsoiud aasjlj sdlkja dlkja da asdadas alsajdlaksjdlajd alsjdalkjd alksjd sa"
Style="{StaticResource LabelValueStyle}" />
<Label
Grid.Row="0"
Grid.Column="1"
Text="Primary Inspector"
Style="{StaticResource LabelKeyStyle}" />
<Label
Grid.Row="1"
Grid.Column="1"
Style="{StaticResource LabelValueStyle}" >
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding Path=InspectorFirstName}"/>
<Span Text=" "/>
<Span Text="{Binding Path=InspectorLastName}"/>
</FormattedString>
</Label.FormattedText>
</Label>
</Grid>
<Grid Grid.Row="2"
Grid.Column="0"
Grid.ColumnSpan="2"
RowDefinitions="*, *"
ColumnDefinitions="*, *">
<Label
Grid.Row="0"
Grid.Column="0"
Text="Scheduled Date"
Style="{StaticResource LabelKeyStyle}" />
<Label
Grid.Row="1"
Grid.Column="0"
Text="{Binding Path=ScheduledStartDate, Converter={StaticResource dateFormatter},ConverterParameter='long'}"
Style="{StaticResource LabelValueStyle}" />
<Label
Grid.Row="0"
Grid.Column="1"
Text="Completed Date"
Style="{StaticResource LabelKeyStyle}" />
<Label
Grid.Row="1"
Grid.Column="1"
Text="{Binding Path=CompletionDate, Converter={StaticResource dateFormatter},ConverterParameter='long'}"
Style="{StaticResource LabelValueStyle}" />
</Grid>
</Grid>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<StackLayout
Grid.Row="1"
VerticalOptions="End"
HorizontalOptions="FillAndExpand"
BackgroundColor="{StaticResource White}">
<controlTemplate:BeginInspectionContentView></controlTemplate:BeginInspectionContentView>
</StackLayout>
</Grid>
</StackLayout>
Output & expected output image attached here:
How to achieve dynamic height for this UI? Any help or suggestions are welcome.
There are three solutions to this problem:
You can change CollectionView to FlexLayout with BindableLayout.ItemsSource and BindableLayout.ItemTemplate. Then you don't need to set Height for item or FlexLayout because all are dynamic.
You can set a value to collectionView.HeightRequest using MVVM. When item increase, the height will increase as well. For more details about this solution, you may refer to the link.
You can use CollectionView.Behaviors to calculate the Height of every item to increase the height of ContentView. For more details about this solution, you may refer to the link.
You have Grid. With StackLayout. With Grid. With Small Grids inside it.
I will put aside for now, that this is extremely bad for rendering. (It is serious problem however...)
None of the expected output justify this organization.
If you need for element to occupy 2 columns, you can use columns span 2.
Before anything else, please combine all 4 grids in a single grid.
After you are done with that, limit your VisualElements height, or, even better, limit the content you are putting in them. (Because cut content looks bad. You should be using something like "show more" link, that opens popup or whatever).
If you have any questions, please ask.
Edit: Let me clarify:
You have the same Grid code. Most important part - columns are * , * , and rows are auto height. And every 2 rows, you create another GRID, that is copy of the first GRID.
This is extremely counterproductive.
Also, just because you can do something, and it is "working in xamarin", it doesn't mean that you should do it.
First, put everything in the same grid, instead of nesting it and copying grids, then you can start working on CLIP/Height limit/etc.
(Recommending you to test it on IOS, because once you get it running fine there, it is much easier to fine-tune it for other platforms, than the other way around.)

Resize Frame Within Grid Row

I am using xamarin with visual studio for IOS and Android
I am trying to add a scrollView to the same grid row that my Frame is on but because the frame is the same size as my grid row it cuts off the bottom of the frame (I believe)
What I've tried :
1. Setting the RowDefinition to "*" and "Auto" but the frame increases the length with the grid and
2. Adjusting the scrollView height
I feel like my best option would be resizing the frame so it's smaller than the grid row, however heightRequest doesn't work.. I would love any suggestions.
Here is the code:
<Grid HeightRequest="400">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="130"/>
<RowDefinition Height="60"/>
<RowDefinition Height="20"/>
<RowDefinition Height="300"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollView Orientation="Horizontal" Grid.Row="4" Margin="0,20,0,0" HeightRequest="390" HorizontalScrollBarVisibility="Never">
<StackLayout Orientation="Horizontal" Grid.Row="4" BackgroundColor="Red">
<Frame WidthRequest="200" HeightRequest="300" Padding="0,0,0,0" CornerRadius="10" IsClippedToBounds="True" Margin="15,0,0,0">
<StackLayout>
<Image Source="satayChicken.jpg" WidthRequest="200" HeightRequest="200" Aspect="AspectFill"></Image>
<Label Text="Satay Chicken" FontSize="24" TextColor="#48b67b" HorizontalOptions="Center" Margin="0,-5,0,0"/>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center">
<Label Text="PREP TIME" FontSize="12" TextColor="#4a6356" />
<Label Text="COOK TIME" FontSize="12" TextColor="#4a6356" Margin="5,0,0,0"/>
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center" Margin="0,-10,0,0">
<Label Text="5 mins" FontSize="14" TextColor="#48b67b" />
<Label Text="12 mins" FontSize="14" TextColor="#48b67b" Margin="15,0,0,0"/>
</StackLayout>
</StackLayout>
</Frame>
</StackLayout>
</ScrollView>
</Grid>
<StackLayout Orientation="Horizontal" Grid.Row="4" BackgroundColor="Red">
as you have defined the StackLayout Orientaition="Horizontal",the Frame will fill the height by default.
if you want to resize the Frame height,you sholud set its VerticalOptions property
like :
<ScrollView Orientation="Horizontal" Grid.Row="4" Margin="0,20,0,0" HorizontalScrollBarVisibility="Never">
<StackLayout Orientation="Horizontal" BackgroundColor="Red" >
<Frame VerticalOptions="CenterAndExpand" HeightRequest="270" WidthRequest="200" Padding="0,0,0,0" CornerRadius="10" IsClippedToBounds="True" Margin="15,0,0,0">
<StackLayout>
<Image Source="satayChicken.jpg" WidthRequest="200" HeightRequest="200" Aspect="AspectFill"></Image>
<Label Text="Satay Chicken" FontSize="24" TextColor="#48b67b" HorizontalOptions="Center" Margin="0,-5,0,0"/>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center">
<Label Text="PREP TIME" FontSize="12" TextColor="#4a6356" />
<Label Text="COOK TIME" FontSize="12" TextColor="#4a6356" Margin="5,0,0,0"/>
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center" Margin="0,-10,0,0">
<Label Text="5 mins" FontSize="14" TextColor="#48b67b" />
<Label Text="12 mins" FontSize="14" TextColor="#48b67b" Margin="15,0,0,0"/>
</StackLayout>
</StackLayout>
</Frame>
</StackLayout>
</ScrollView>
The specific size is set according to your requirements.
or you also could set Padding property to the StackLayout to resize the Frame location like:
<StackLayout Padding="0,10,0,10" Orientation="Horizontal" Grid.Row="4" BackgroundColor="Red">
<Frame WidthRequest="200" HeightRequest="300" Padding="0,0,0,0" CornerRadius="10" IsClippedToBounds="True" Margin="15,0,0,0">
<StackLayout>
<Image Source="satayChicken.jpg" WidthRequest="200" HeightRequest="200" Aspect="AspectFill"></Image>
<Label Text="Satay Chicken" FontSize="24" TextColor="#48b67b" HorizontalOptions="Center" Margin="0,-5,0,0"/>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center">
<Label Text="PREP TIME" FontSize="12" TextColor="#4a6356" />
<Label Text="COOK TIME" FontSize="12" TextColor="#4a6356" Margin="5,0,0,0"/>
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center" Margin="0,-10,0,0">
<Label Text="5 mins" FontSize="14" TextColor="#48b67b" />
<Label Text="12 mins" FontSize="14" TextColor="#48b67b" Margin="15,0,0,0"/>
</StackLayout>
</StackLayout>
</Frame>
</StackLayout>

Align label and switch in xamarin forms

I want to align the label and switch in xamarin forms. I used grid but the label and switch wont align (see image for reference)
Here is my XAML:
<Grid>
<Label Grid.Row="0" Grid.Column="0" Text="Rekorida" StyleClass="lbl-fieldform">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="Android" Value="HelveticaNeueLTPro-Lt.otf#HelveticaNeueLTPro-Lt"/>
</OnPlatform>
</Label.FontFamily>
</Label>
<Switch Grid.Row="0" Grid.Column="1" VerticalOptions="Start" x:Name="swRekorida" Toggled="Activity_Toggled"/>
</Grid>
Use this:
<Grid VerticalOptions="Center">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Label VerticalTextAlignment="Center" Grid.Row="0" Grid.Column="0" Text="Rekorida" StyleClass="lbl-fieldform">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="Android" Value="HelveticaNeueLTPro-Lt.otf#HelveticaNeueLTPro-Lt"/>
</OnPlatform>
</Label.FontFamily>
</Label>
<Switch Grid.Row="0" Grid.Column="1" VerticalOptions="Center" x:Name="swRekorida" Toggled="Activity_Toggled"/>
</Grid>

How can I stop my labels going on top of one another?

Here is my XAML:
<Grid x:Name="emptyGrid" Grid.Row="0" Grid.Column="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="80*" />
<RowDefinition Height="20*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Padding="20" HorizontalOptions="Center" VerticalOptions="Center">
<Label x:Name="emptyLabel1" FontSize="18" XAlign="Center" TextColor="Gray" />
<Label x:Name="emptyLabel2" FontSize="18" XAlign="Center" TextColor="Gray" />
<Label x:Name="emptyLabel3" FontSize="18" XAlign="Center" TextColor="Gray" />
</Grid>
All my 2-5 line labels appear on top of each other but what I would like is for them to evenly fill out the space given to them:
Use a StackLayout which will lay them down next to each other horizontally or vertically and set VerticalOptions so that they fill the space they're given:
<Grid Grid.Row="0" Padding="20" HorizontalOptions="Center" VerticalOptions="Center">
<StackLayout>
<Label x:Name="emptyLabel1" FontSize="18" XAlign="Center" TextColor="Gray" VerticalOptions="FillAndExpand" />
<Label x:Name="emptyLabel2" FontSize="18" XAlign="Center" TextColor="Gray" VerticalOptions="FillAndExpand" />
<Label x:Name="emptyLabel3" FontSize="18" XAlign="Center" TextColor="Gray" VerticalOptions="FillAndExpand" />
</StackLayout>
</Grid>

ContentPage containing list view display very slow on Android

I am not sure whether this is my problem or a problem with Xamarin.
On an iPad my application can load the page which contain a list of data in ListView.
The View Cell inside the ListView can be slightly complex. It contains a profile image, some smaller icons using, which are using FFImageLoading.
On iPad, when I tap a cell of the first page to go into the next page, which contains a list of data, it loads very smoothly.
On Android, when I tap on a cell to go into the next page, the detection of the tap is slow and the loading time of the ListView also very slow.
Is there anyway enhance the performance? I thought it was the data I loaded from SQLite. However, after I comment out the ItemsSource on ListView, the loading time is fine.
This is the datatemplate I've used for displaying the cell.
<ViewCell>
<ViewCell.View>
<StackLayout Spacing="0" Padding="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<StackLayout Orientation="Horizontal" Spacing="10" Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand">
<controls:CircleImage
Style="{StaticResource profileImageStyle}"
Margin="10, 10, 10, 10"
Source="{Binding Source}"
BorderColor="White"
BorderThickness="2"
VerticalOptions="Center"
HorizontalOptions="Center">
<controls:CircleImage.GestureRecognizers>
<TapGestureRecognizer Tapped="OnChildDetailTapped" />
</controls:CircleImage.GestureRecognizers>
</controls:CircleImage>
<StackLayout VerticalOptions="Fill" Spacing="1" Padding="0,20,0,10" HorizontalOptions="StartAndExpand">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnChildDetailTapped" />
</StackLayout.GestureRecognizers>
<StackLayout
Orientation="Vertical"
VerticalOptions="Center"
HorizontalOptions="StartAndExpand">
<Label x:Name="childName" Text="{Binding DisplayName}" Style="{StaticResource normalFont}"> </Label>
<local:ChildInfoIconsView
Child="{Binding .}"
VerticalOptions="Fill">
</local:ChildInfoIconsView>
<Label
x:Name="childNotes"
Style="{StaticResource footnoteFont}"
Text="{Binding ChildNotes, StringFormat={x:Static local:AppResources.formatNotes}}"
IsVisible="{Binding HasChildNotes}">
</Label>
<Label
x:Name="noPickupReason"
Style="{StaticResource footnoteFont}"
Text="{Binding NoPickupReason, StringFormat={x:Static local:AppResources.formatNoPickupReason}}"
IsVisible="{Binding HasNoPickupReason}">
</Label>
<Label
x:Name="absentReason"
Style="{StaticResource footnoteFont}"
Text="{Binding AbsentReason, StringFormat={x:Static local:AppResources.formatAbsentReason}}"
IsVisible="{Binding HasAbsentReason}">
</Label>
</StackLayout>
</StackLayout>
</StackLayout>
<StackLayout Orientation="Horizontal" VerticalOptions="FillAndExpand" HorizontalOptions="End">
<StackLayout.Padding>
<OnIdiom x:TypeArguments="Thickness">
<OnIdiom.Phone>5, 20, 10, 20</OnIdiom.Phone>
<OnIdiom.Tablet>10, 30, 30, 30</OnIdiom.Tablet>
</OnIdiom>
</StackLayout.Padding>
<Image
Style="{StaticResource listviewButtonStyle}"
IsVisible="{Binding EnabledSigning, Source={x:Reference page}}"
Source="ic_action_yes.png"
VerticalOptions="FillAndExpand"
HorizontalOptions="End">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnAttend" />
</Image.GestureRecognizers>
<Image.Margin>
<OnIdiom x:TypeArguments="Thickness">
<OnIdiom.Phone>0, 0, 5, 0</OnIdiom.Phone>
<OnIdiom.Tablet>5, 5, 20, 5</OnIdiom.Tablet>
</OnIdiom>
</Image.Margin>
</Image>
<Image
Style="{StaticResource listviewButtonStyle}"
IsVisible="{Binding EnabledSigning, Source={x:Reference page}}"
Source="ic_action_no.png"
VerticalOptions="FillAndExpand"
HorizontalOptions="End">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnAbsent" />
</Image.GestureRecognizers>
<Image.Margin>
<OnIdiom x:TypeArguments="Thickness">
<OnIdiom.Phone>5, 0, 0, 0</OnIdiom.Phone>
<OnIdiom.Tablet>20, 5, 5, 5</OnIdiom.Tablet>
</OnIdiom>
</Image.Margin>
</Image>
</StackLayout>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
Try setting a listview caching strategy to improve performance.
See here for more details
Be aware though that you may need to jump through some hoops to use RecycleElement with FFImageLoading. The issue is described here
Also, please post your data template so we can see if it can be simplified.
Use RecyclerView
https://blog.xamarin.com/recyclerview-highly-optimized-collections-for-android-apps/
OR
Use FFImageLoading
https://github.com/luberda-molinet/FFImageLoading
to optimize and for smoother scroll

Resources