How to use a Frame as the root element of a ListView in Xamarin.Forms? - xaml

I am trying to achieve a list of Card-like records for the timeline on an application built in Xamarin.Forms, this is the layout that more or less I need to replicate:
I think I am close, I have two records on my code behind that I assign as the ItemSource to a listView on the XAML side, and this is my result so far:
But as you can see, the elements are not showing at all inside their frame, if I replace the form controls with TextCells or ImageCells, the data shows fine. This is the XAML code I have so far:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage Title="AudioBitts"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:AudioBitts"
x:Class="AudioBitts.TimelinePage">
<ListView x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Height="300">
<Frame HasShadow="true" Margin="5">
<StackLayout>
<StackLayout Orientation="Horizontal">
<Image Source="{Binding UserAvatar}" />
<Label Text="{Binding UserName}" />
</StackLayout>
<Image Source="{Binding BittImage}" />
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
Am I missing something on the structure I am giving to my XAML code? Thanks!

You can set ListView.RowHeight or set ListView.HasUnevenRow to allow the data to be displayed in full.

Related

How do I force child controls or layouts to be the same width as the parent in Xamarin Forms?

I have the following 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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="NGIC_XAML.Views.Payments.Payments">
<ContentPage.Content>
<FlexLayout Direction="Column" AlignItems="Center" HorizontalOptions="FillAndExpand">
<StackLayout BackgroundColor="Red" HeightRequest="108" HorizontalOptions="FillAndExpand">
<Label Text="Payments" TextColor="#F9F8FA" FontSize="20" Margin="40"
HorizontalTextAlignment="Center" VerticalOptions="Center"
HorizontalOptions="FillAndExpand">
</Label>
</StackLayout>
</FlexLayout>
</ContentPage.Content>
</ContentPage>
I want the outer layout, the FlexLayout, to automatically size the width to the device's maximum. I want the Layout (the StackLayout) and child Control (the Label), to also automatically resize their widths to the FlexLayout. I am stumped as to how to do this. Any suggestions?
In the FlexLayout docs, they mention "The HorizontalOptions property doesn't work for children of a FlexLayout".
If you take a look at the Page layout with FlexLayout section (Holy Grail Layout) you can see an example somewhat similar to what you are trying to accomplish, basically use a nested FlexLayout instead of a StackLayout.
The code would look like:
<ContentPage.Content>
<FlexLayout
BackgroundColor="Yellow"
Direction="Column">
<FlexLayout
HeightRequest="108"
AlignItems="Center"
Direction="Column"
BackgroundColor="Red">
<Label
BackgroundColor="Green"
LineBreakMode="CharacterWrap"
Text="Payments"
TextColor="#F9F8FA"
FontSize="20"
Margin="40"
HorizontalTextAlignment="Center"
VerticalOptions="Center">
</Label>
</FlexLayout>
</FlexLayout>
</ContentPage.Content>
And here is the screenshot:
Notice that the label will grow as needed to use the full width of the row.
To achieve this, please assign HorizontalOptions="FillAndExpand" to StackLayout and Label and other child controls if any.
This will tell the controls to fill all the space available horizontally.
Thanks!

Space around in xamarin forms

Im using xamarin forms.
i wanna design my xaml files but i realized something strange.
left and right of entry has space that i couldnt remove even with custom renderer.
is there any idea what is might be?
i tried padding,margin,HorizontalOption none of them effected.
even i tried to setPadding to 0 from custom renderer in android but it didnt work.
the first one is label as you can see filled out all the space but entry has an space around it .
Update:
this is my xaml codes and im using android emulator to test.
<StackLayout BackgroundColor="White" Spacing="15">
<Label Text="fds" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Red"/>
<Entry BackgroundColor="Red" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
</StackLayout>
don't know how is your implementation but this what I tried:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns:bases="clr-namespace:WhiteSolution.Views.Bases"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModelBase="clr-namespace:WhiteSolution.ViewModels.Bases"
viewModelBase:ViewModelLocator.AutoWireViewModel="True"
x:Class="WhiteSolution.Views.HomePage">
<ContentPage.Content>
<Grid VerticalOptions="FillAndExpand"
BackgroundColor="WhiteSmoke">
<StackLayout Padding="0"
Spacing="0"
BackgroundColor="Blue">
<Label Text="no space label"
Margin="0"
HorizontalOptions="FillAndExpand"
HorizontalTextAlignment="Center"
BackgroundColor="Yellow"
FontSize="Large"/>
<Entry Text="no space entry"
Margin="0"
BackgroundColor="Red"
HorizontalOptions="FillAndExpand"
HorizontalTextAlignment="Center"
FontSize="Large"/>
</StackLayout>
</Grid>
</ContentPage.Content>
</ContentPage>
and i get this result
if you share your code it will be more useful
Try this, Padding Property should be 0 in main StackLayout.
<?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="SampleApp.Views.DashboardView">
<ContentPage.Content>
<StackLayout Padding="0" BindingContext="{Binding Dashboard}">
<!-- Controls -->
</StackLayout>
</ContentPage.Content>
I realized that entry has Default margin value. so i set it to 0 and everything worked.
here is my code
<StackLayout>
<Entry Margin="0" Text="fd" BackgroundColor="Red" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
<Label Text="fd" BackgroundColor="Red" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
</StackLayout>

Hide frame in custom ContentView

I'm using a content view page called CardViewTemplate.xaml in my ListView to render some content with two frames, one of them should be visible and the other not, at the same time.
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
...>
<Frame IsVisible="{Binding IsNotBreak}" IsClippedToBounds="True">
.... content ....
</Frame>
<Frame IsVisible="{Binding IsBreak}" IsClippedToBounds="True">
--content 2-
</Frame>
</ContentView>
and the above page used in list view to bind and view some data to listview like below code:
<ListView x:Name="listView" SelectedItem="{Binding SelcetedItem,Mode=TwoWay}"
RowHeight="-1"
ItemsSource="{Binding SelectedCard}" HasUnevenRows="True" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Custom:CardViewTemplate/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Unexpectedly my list view shows only the second frame and it looks like attached image:
That is because ContentView is supposed to have only one child, and the second Frame is overriding the first Frame.
Try adding a view-container like AbsoluteLayout, or Grid to hold the two frame(s) inside ContentView:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
...>
<AbsoluteLayout>
<Frame IsVisible="{Binding IsNotBreak}" IsClippedToBounds="True">
.... content ....
</Frame>
<Frame IsVisible="{Binding IsBreak}" IsClippedToBounds="True">
--content 2-
</Frame>
</AbsoluteLayout>
</ContentView>

The cell doesnt show a complete image

I want to do a Cell with a yellow background and one orange box with a text and after an image with a medium size, but it doesn't work. The image doesn't appear big and I can't use a margin
This is my 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"
x:Class="neoFly_Montana.Views.PromocoesView"
BackgroundImage="promocoesbackground"
Title="Promoções">
<ContentPage.Content>
<ListView x:Name="listview_promocoes" Margin="0, 20, 0, 0 ">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="AliceBlue" HorizontalOptions="Center" VerticalOptions="FillAndExpand">
<Label Text="{Binding titulo}" Style="{StaticResource labelsfont}"/>
<Image Source="{Binding imagem}" Aspect="AspectFill"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
This is the bad result (with 2 item - u can't see the image)
I want to do something like this but I can't use margin because when I use it, the app doesn't work.
It needs to be one item of a ListView.
All items should be like this.
How can I do that?
Thank you.
You can get that Layout in your ListView item with something like this:
<ListView
RowHeight="170"
ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<!-- Outter box containing two boxes -->
<StackLayout
VerticalOptions="FillAndExpand">
<!-- First box with Title, Image and Text -->
<StackLayout
VerticalOptions="FillAndExpand"
Padding="10, 5"
BackgroundColor="Yellow">
<Label
Text="YOUR_TITLE"
BackgroundColor="Silver"/>
<Image
HorizontalOptions="FillAndExpand"
BackgroundColor="Fuchsia"
HeightRequest="60" />
<Label
Text="YOUR_OTHER_TEXT"
BackgroundColor="Silver"/>
</StackLayout>
<!-- Second box with Text -->
<StackLayout
VerticalOptions="End"
Margin="0, 8, 0, 0"
Padding="10, 5"
BackgroundColor="Lime">
<Label
Text="YOUR_TEXT_OTHER_BOX"
BackgroundColor="Silver"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I guess you might need to do a few little changes to get it exactly as you want it but this is pretty close.
Result:
Hope this helps.-

How to change icon color in Xamarin.Forms xaml page?

Currently, I am developing a Xamarin.Forms PCL application for Android and iOS. In one of my pages I define a ListView. The template for the ListView.ItemTemplate displays an icon using <Image Source="some_Icon.png" />. The icon displays as expected (black) but I have not found a way to set the color.
I would like to colorize this icon based on logic in my ViewModel.
I have searched for answers both through the Documentation and StackOverlflow to no avail.
Is there a better way to display and style icons from a Xamarin.Forms PCL project?
Does Xamarin.Forms require a set of resource/image files for every possible color I might use?
I solved my problem with Iconize for Xamarin.Forms. I implemented my solution based off the Iconize sample code 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"
xmlns:iconize="clr-namespace:FormsPlugin.Iconize;assembly=FormsPlugin.Iconize"
x:Class="Iconize.FormsSample.Page1"
Title="{Binding FontFamily}">
<ContentPage.ToolbarItems>
<iconize:IconToolbarItem Command="{Binding ModalTestCommand}" Icon="fa-500px" />
<iconize:IconToolbarItem Command="{Binding VisibleTestCommand}" Icon="fa-500px" IconColor="Red" />
<iconize:IconToolbarItem Command="{Binding VisibleTestCommand}" Icon="fa-500px" IsVisible="{Binding VisibleTest}" />
</ContentPage.ToolbarItems>
<ListView CachingStrategy="RecycleElement" ItemsSource="{Binding Characters}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<iconize:IconImage HeightRequest="20" Icon="{Binding Key}" IconColor="Blue" WidthRequest="20" />
<iconize:IconImage HeightRequest="20" Icon="{Binding Key}" BackgroundColor="Blue" IconColor="Yellow" WidthRequest="20" IconSize="10" />
<iconize:IconButton FontSize="20" Text="{Binding Key}" TextColor="Red" WidthRequest="48" />
<iconize:IconLabel FontSize="20" Text="{Binding Key}" TextColor="Green" VerticalTextAlignment="Center" />
<Label Text="{Binding Key}" VerticalTextAlignment="Center" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
In my case, I simply had to replace my original <Image .../> element and replace it with
<iconize:IconImage HeightRequest="20" Icon="fa-circle" IconColor="{Binding CircleColor}" WidthRequest="20" />.
In my ViewModel, I added the CircleColor property to set the icon color as needed based on my logic.