I'm facing some tricky bugs while trying to use the new CollectionView
feature implemented in Xamarin Forms 4. On the Android
project it works very
well after enabling experimental features in MainActivity.cs with:
global::Xamarin.Forms.Forms.SetFlags(new[] { "CollectionView_Experimental", "Shell_Experimental" });
But xamarin
documentation doesn't provide any information about UWP project
so first when I tried to compile the UWP project, it throws me this exception
when it tries to render a page that uses CollectionView
System.InvalidOperationException:
'The class, property, or method you are attempting to use ('VerifyCollectionViewFlagEnabled') is part of CollectionView; to use it, you must opt-in by calling Forms.SetFlags("CollectionView_Experimental") before calling Forms.Init().'
So I tried to call the SetFlags in App.xaml.cs in UWP project before calling
InitializeComponent() method. So this time it throws me this exception when
it tries to load the page containing CollectionView
:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
So like in this example:
await Navigation.PushAsync(new PageWithCollectionView());
The exception is thrown after successful execution of PageWithCollectionView constructor.
Can somebody help me to solve this problem?
UPDATE
Ok, so adding the SetFlags in App.xaml.cs in UWP project works and the CollectionView gets initialized correctly. But the NRE is still there (on Android the CollectionView works without problems), while trying to get rid of this problem I noticed that it's caused when I try to nest the XAML layout this way:
<CollectionView SelectionMode="Single">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Horizontal"
Span="2"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<StackLayout Orientation="Vertical"
Grid.Column="0"
Grid.Row="0">
<Label Text="{Binding Title}"/>
</StackLayout>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
If I remove all the DataTemplate from CollectionView.ItemTemplate, just leaving it blank like this:
<CollectionView SelectionMode="Single">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Horizontal"
Span="2"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
The page gets rendered a CollectionView
shows ItemSource elements with messy
layout (without margin and padding, and calling ToString()
method of element
to visualize it inside the cell).
[UPDATE]
After update to Xamarin Forms
4 pre 8, the exception is gone.
CollectionView
was not supported in UWP. – Summertree