Xamarin Forms CollectionView Custom Layout Design - xamarin

In the app that I'm designing, I use collectionview to show loads of images. And this is my current design.
And this is the design I want.
where the images that are shorter in height will have the next image fill up the white spaces.
Heres my code:
<CollectionView x:Name="cv_WallPapers"
ItemsSource="{Binding Results, Mode=TwoWay}"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
SelectionMode="Single"
SelectionChanged="cv_WallPapers_SelectionChanged"
>
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="2"
VerticalItemSpacing="5"
HorizontalItemSpacing="5"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame Padding="4"
BorderColor="#34eba8"
BackgroundColor="#34eba8"
CornerRadius="10"
HasShadow="False">
<Image Source="{Binding urls.regular}"
HorizontalOptions="FillAndExpand"/>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
I've no idea how to do that in Xamarin. And appreciate the help!

Unfortunately,CollectionView in Forms still doesn't support such a layout until now .However, we could use Custom Renderer as a workaround in Android.
public class MyCollectionViewRenderer: CollectionViewRenderer
{
public MyCollectionViewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<ItemsView> elementChangedEvent)
{
base.OnElementChanged(elementChangedEvent);
if(elementChangedEvent.NewElement!=null)
{
this.SetLayoutManager(new StaggeredGridLayoutManager(2, 1));
}
}
}
In iOS , because the UICollectionView in native iOS doesn't have such a property or API , so we need to implement it manually (re-calculate the size of each item and set the offset) . Which you could refer https://developer.apple.com/documentation/uikit/uicollectionview/customizing_collection_view_layouts?language=objc . And I will post the feature request as soon as possible .

Related

Xamarin Image not Loading

I am trying to display a list of images with different sizes and i want to display them with there full height and width, but the images are not loading on the screen if i didn't specified the height in the image control.
code :
<Grid>
<CollectionView ItemsSource="{Binding ImgList}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid RowDefinitions="Auto,Auto" ColumnDefinitions="Auto,*">
<Label>Image:</Label>
<Image Grid.Row="1" Grid.ColumnSpan="2" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill" Source="{Binding .}"></Image>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
note: ImgList is an observableCollection of Images name string.
You could refer to the code below. You could check the image location and binding. I put the image in Android Resource/Drawable folder.
Xaml: The same xaml with yours.
Code behind:
public ObservableCollection<string> ImgList { get; set; }
public Page10()
{
InitializeComponent();
ImgList = new ObservableCollection<string>() { "image.jpeg", "image.jpeg" };
this.BindingContext = this;
}
Can you please make sure if you load images from your code project than you need to give Whole Path from root.
Can you please also check that Build Action of images.

Xamarin form carouselview scrolls with multiple items for single scroll

Iam using Carousel view control available in Xamarin form(Version 5).
In my case i used Listviews inside carousel view. It works fine if scroll by swiping to left or right, but if i programmatically set the ScrollTo or Position property for the carousel view using button click then it is scrolling multiple times though i'm setting single incremental value.
I added SnapPointsType="MandatorySingle" as mentioned here
One more issue is if my current carousel position is zero and moving to last position or more then one is not working.
<AbsoluteLayout Margin="2" BackgroundColor="Transparent">
<CarouselView
x:Name="MyCarousalView"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All"
Scrolled="CarouselViewScrolled">
<CarouselView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal" ItemSpacing="0" SnapPointsType="MandatorySingle" />
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate>
<ListView
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
ItemsSource="{Binding .}"
x:FieldModifier="public">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Tapped="ViewCellTapped">
<StackLayout Padding="0">
<StackLayout Padding="20,1" VerticalOptions="StartAndExpand" Spacing="0" >
<Label
FontSize="16"
Text="{Binding Description}"
FontAttributes="Bold"/>
<Label
FontSize="14"
Text="{Binding CreatedDate}" />
</StackLayout>
<Label HorizontalOptions="FillAndExpand" Padding="0" HeightRequest="1" BackgroundColor="#D4D4D4"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</AbsoluteLayout>
Next button click event
private void NextClicked(object sender, EventArgs e)
{
MyCarousalView.ScrollTo((MyCarousalView.Position + 1) < _weekDaysList.Count ? MyCarousalView.Position + 1 : 0);
}
Carousel view binding:
List<List<Note>> _myList;
_myList=getData(); //based on service response
MyCarousalView.ItemsSource = null;
MyCarousalView.ItemsSource = _myList;
Screen recording with issue image
Screen video
Please letme know if any solution/workaround. Thanks

xamarin collectionview cell custom border

How to add custom border to the collectionview cell like the image?
<CollectionView HeightRequest="100" ItemsSource="{Binding Test}">
<CollectionView.ItemsLayout >
<GridItemsLayout Orientation="Vertical" Span="4"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid >
<Image Source="{Binding Image}" ></Image>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Put the collection view into a colored container could do that, and set margin for the collection view as the width of border, like
<StackLayout BackgroundColor="Black" HorizontalOptions="FillAndExpand">
<CollectionView Margin="4,0">
</CollectionView>
</StackLayout>
I use a sub-class frame to add border, please take a look at the following code:
Creating custom frame in .Net standard library project(shared code).
public class MyFrame : Xamarin.Forms.Frame
{
}
Custom renderer in Android platform.
[assembly: ExportRenderer(typeof(MyFrame), typeof(MyFrameRenderer))]
namespace demo3.Droid
{
[Obsolete]
class MyFrameRenderer : VisualElementRenderer<Frame>
{
public MyFrameRenderer()
{
SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.blue_rect));
}
}
}
<ContentPage.Content>
<StackLayout HorizontalOptions="FillAndExpand">
<control:MyFrame Padding="5">
<CollectionView BackgroundColor="Transparent" ItemsSource="{Binding Test}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="4" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame BackgroundColor="Transparent" BorderColor="Gray">
<Label Text="{Binding .}" TextColor="Black" />
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</control:MyFrame>
</StackLayout>
</ContentPage.Content>
The screenshot:

Control template Binding Not working in MVVM Cross Xamarin Forms

I am currently working on a Xamarin Forms project which is using MVVMCross. In App.xaml i created a control template as follows
<ControlTemplate x:Key="ContentPageTemplate">
<Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" RowSpacing="0" RowDefinitions="Auto,Auto,*">
<!--StackLayout as Header-->
<StackLayout Grid.Row="0" HeightRequest="6" BackgroundColor="{StaticResource SecondaryBrandColor}"></StackLayout>
<Frame Padding="0" Grid.Row="1" >
<Label Grid.Row="1" Text="Server Not Available" BindingContext="{TemplateBinding BindingContext}"
Style="{StaticResource ServerAvailableLabel}"
IsVisible="{TemplateBinding Parent.BindingContext.IsServerAvailableLabelVisible}"/>
</Frame>
<!--Content Page Body-->
<ContentPresenter Grid.Row="2" BackgroundColor="{StaticResource PrimaryBodyColor}">
</ContentPresenter>
</Grid>
</ControlTemplate>
Now i am using this control template in contentPage as follows
<views:MvxContentPage xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Project.UI.Views.ReqView"
ControlTemplate="{StaticResource ContentPageTemplate}"
Title="Reqs">
</views:MvxContentPage>
In the view model i am trying to make the label visible and invisible as follows
public bool _isServerAvailableLabelVisible = false;
public bool IsServerAvailableLabelVisible
{
get
{
return _isServerAvailableLabelVisible;
}
set
{
SetProperty(ref _isServerAvailableLabelVisible, value);
}
}
The problem i am facing now is, by default the label is being visible and also it is not updating even when i am setting the value as false.
What am i missing ?
I had exactly the same issue !
After one day I found the following solution which works for me.
I added BindingContext="{TemplateBinding BindingContext.DataContext}"into the Grid and change the IsVisible property by IsVisible="{Binding IsServerAvailableLabelVisible}"
Here the recap that you can try, hope it will work for you too !
For information I use Xamarin.Forms 4.8.0 & MvvmCross 6.3.1
<ControlTemplate x:Key="ContentPageTemplate">
<Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" RowSpacing="0" RowDefinitions="Auto,Auto,*"
BindingContext="{TemplateBinding BindingContext.DataContext}">
<!--StackLayout as Header-->
<StackLayout Grid.Row="0" HeightRequest="6" BackgroundColor="{StaticResource SecondaryBrandColor}"></StackLayout>
<Frame Padding="0" Grid.Row="1" >
<Label Grid.Row="1" Text="Server Not Available"
Style="{StaticResource ServerAvailableLabel}"
IsVisible="{Binding IsServerAvailableLabelVisible}"/>
</Frame>
<!--Content Page Body-->
<ContentPresenter Grid.Row="2" BackgroundColor="{StaticResource PrimaryBodyColor}">
</ContentPresenter>
</Grid>
</ControlTemplate>
Are you sure that the OnPropertyChanged Event gets fired when you chcange the value?
If yes then you need to check the binding context of the view
If no you need to fire it manually (as mentioned in the MVVMCross documentation)
private string _myProperty;
public string MyProperty
{
get => _myProperty;
set
{
_myProperty = value;
RaisePropertyChanged(() => MyProperty);
// take any additional actions here which are required when MyProperty is updated
}
}

Xamarin Forms CollectionView with square images

I want to display list of images as a grid with several rows (2 or 4) with Xamarin Forms. Each cell of the grid must be square. I'm using CollectionView with vertical layout, required span and fixed HeightRequest in DataTemplate. I get multicolumn grid, but I cannot make images (cells) to be squared.
<CollectionView ItemsSource="{Binding .}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="2" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Image
HeightRequest="100"
x:Name="imageCell"
Aspect="AspectFill"
Source="{Binding .}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Thanks to other answers, I've ended up with custom ContentView setting up HeightRequest equal to Width and the image inside it.
public class SquareView : ContentView
{
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
HeightRequest = Width;
}
}
and XAML
<CollectionView
ItemsSource="{Binding .}">
<CollectionView.ItemsLayout>
<GridItemsLayout
Orientation="Vertical"
Span="2" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<views:SquareView>
<Image
HeightRequest="20"
Aspect="AspectFill"
Source="{Binding .}" />
</views:SquareView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
I've reproduced a small sample.
Only thing I added was to wrap the image inside a contentview, to apply a padding (which you can ignore)
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="2" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<ContentView Padding="0">
<Image Source="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSzJUY4JZTX9oYsSl1jclFvdsoXhtA0AfxqKkYX2P81Qb3cyX9o"
HeightRequest="150"
Aspect="AspectFill"
/>
</ContentView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Don't use <Image></Image> as your root layout in DataTemplate.
Instead, use one of the Layout controls (StackLayout, AbsoluteLayout etc.) or ContentView and then apply some styles or whatever you prefer to this root layout of your CollectionView.
<DataTemplate>
<StackLayout>
<Image
HeightRequest="100"
x:Name="imageCell"
Aspect="AspectFill"
Source="{Binding .}" />
</StackLayout>
</DataTemplate>

Resources