Two views for nested collectionView in xamarinforms

Kholoud Ali 1 Reputation point
2022-06-14T11:31:15.987+00:00

I tried to add multiple views for collectionView in Xamarin.forms
I Added 2 collection views with the same DataSource binding and toggle visibility between them using the button
the datasource is an Observablecollection of objects and each object has a day and list of course

  ObservableCollection ScheduleCollection = new ObservableCollection<ScheduleCollection>();  

and the ScheduleCollection class

 public class ScheduleCollection {  
      
    public String day { set; get; }  
    public List<Schedule> scheduleListByDay { set; get; }}  

so I using nested CollectionView to view this kind of data

it's working in separate contentpages correctly.
but when I added it on the same page it worked on iOS and doesn't work on android and it's crash, and this appears in the output window

 explicit concurrent copying GC freed 21116(1130KB) AllocSpace objects, 3(60KB) LOS objects, 49% free, 6465KB/12MB, paused 259us total 15.930ms  
[libc] Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x7fff35e6dff0 in tid 7067 (app), pid 7067 (app)  

and the xaml code

    <ContentPage.Content>  
    <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" RowDefinitions="Auto,*,Auto"   Padding="0">  

        <!-- The Views options -->  
        <StackLayout Orientation="Horizontal" HorizontalOptions="EndAndExpand" Padding="10,10,10,0" Grid.Row="0" >  
            <!--CouralView-->  
            <Button   x:Name="ScheduleCarouselviewBtn" WidthRequest="60" FontFamily="{StaticResource RegularFont}" FontSize="22" CornerRadius="5" Style="{DynamicResource WhiteButton}" VerticalOptions="Center" Text="{x:Static fontIcons:FontsHelper.ListAlt}" Clicked="ScheduleViewOption_Clicked"></Button>  
            <BoxView WidthRequest="2"></BoxView>  
            <!--Grid View-->  
            <Button x:Name="ScheduleGridViewBtn" FontFamily="{StaticResource RegularFont}"  WidthRequest="60" FontSize="22" CornerRadius="5" Style="{DynamicResource PrimaryButton}" VerticalOptions="Center" Text="{x:Static fontIcons:FontsHelper.CalendarAlt}" Clicked="ScheduleViewOption_Clicked"></Button>  
        </StackLayout>  

        <!--The Schedule | Table View-->  
        <Grid Grid.Row="1" x:Name="scheduleTableCollectionView" >  
            <ScrollView  VerticalScrollBarVisibility="Always" >  
                <CollectionView  ItemsSource="{Binding ScheduleCollection}"    VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" >  
                    <CollectionView.ItemsLayout>  
                        <LinearItemsLayout Orientation="Horizontal" ItemSpacing="2" />  
                    </CollectionView.ItemsLayout>  
                    <CollectionView.EmptyView>  
                        <Label RotationY="{OnPlatform iOS=180}" BackgroundColor="{DynamicResource PageBackgroundColor}" FlowDirection="RightToLeft"  TextColor="{StaticResource GrayColor}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" Text="" />  
                    </CollectionView.EmptyView>  
                    <CollectionView.ItemTemplate>  
                        <DataTemplate>  
                            <Grid >  
                                <Grid.RowDefinitions>  
                                    <RowDefinition Height="Auto"/>  
                                    <RowDefinition Height="*"/>  
                                </Grid.RowDefinitions>  
                                <Grid.ColumnDefinitions>  
                                    <ColumnDefinition  Width="70"/>  
                                </Grid.ColumnDefinitions>  
                                <Label Grid.Row="0" Text="{Binding day}" Style="{DynamicResource greenLabel}" FontSize="Small" FontAttributes="Bold"  HorizontalOptions="FillAndExpand" WidthRequest="100" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" BackgroundColor="{DynamicResource PageBackgroundColor}"  />  

                                <CollectionView Grid.Row="1" x:Name="SchView" ItemsSource="{Binding scheduleListByDay}"  BackgroundColor="{DynamicResource ScheduleBackgroundColor}"  HeightRequest="{Binding DayCoursesCollectionHieght}" SelectionMode="Single" SelectionChanged="SchView_SelectionChanged"  HorizontalOptions="CenterAndExpand" FlowDirection="RightToLeft" >  
                                    <CollectionView.ItemsLayout>  
                                        <GridItemsLayout VerticalItemSpacing="4"  Orientation="Vertical" />  
                                    </CollectionView.ItemsLayout>  
                                    <CollectionView.EmptyView>  

                                        <StackLayout VerticalOptions="FillAndExpand" >  
                                            <Label RotationY="{OnPlatform iOS=180}" Rotation="-90"  FlowDirection="RightToLeft" VerticalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" Text=""  Style="{DynamicResource scheduleLabels}"/>  
                                        </StackLayout>  
                                    </CollectionView.EmptyView>  
                                    <CollectionView.ItemTemplate>  
                                        <DataTemplate>  
                                            <Frame  CornerRadius="1" BackgroundColor="{Binding CellColor}" Padding="5" BorderColor="Transparent"  >  

                                                <StackLayout Orientation="Vertical" >  
                                                    <Label Text="{Binding STARTTIME}" TextColor="White" HorizontalTextAlignment="Center"  FontSize="Small"  />  
                                                    <Label Text="{Binding ENDTIME}" TextColor="White" HorizontalTextAlignment="Center"  FontSize="Small"  />  
                                                    <Label Text="{Binding COURSE}" TextColor="White"  HorizontalTextAlignment="Center"  FontSize="Small" />  
                                                    <Label Text="{Binding SECTION}" TextColor="White"  HorizontalTextAlignment="Center"  FontSize="Small"/>  

                                                </StackLayout>  

                                            </Frame>  
                                        </DataTemplate>  
                                    </CollectionView.ItemTemplate>  
                                </CollectionView>  

                            </Grid>  

                        </DataTemplate>  
                    </CollectionView.ItemTemplate>  
                </CollectionView>  
            </ScrollView>  
        </Grid>  

        <!--The Schedule | Card View-->  
        <Grid RowDefinitions="0.09*,0.85*" x:Name="scheduleCardCollectionView" Grid.Row="1" IsVisible="true">  
            <!-- Schedule CoursalView -->  
            <CollectionView   x:Name="daysCarouselView" Grid.Row="1" ItemsSource="{Binding ScheduleCollection , Mode=TwoWay}" Margin="10" FlowDirection="RightToLeft" >  
                <CollectionView.ItemsLayout>  
                    <LinearItemsLayout Orientation="Horizontal" ItemSpacing="10"/>  
                </CollectionView.ItemsLayout>  
                <CollectionView.ItemTemplate >  

                    <DataTemplate>  

                        <Frame WidthRequest="280" CornerRadius="10" Margin="10" Style="{DynamicResource GradinateGreanFrameAndDark}" Padding="2">  
                            <StackLayout>  
                                <!--Days Name-->  
                                <Frame HorizontalOptions="FillAndExpand" CornerRadius="10" Style="{DynamicResource GradinateWhiteFrameAndGrean}" HeightRequest="50" Padding="0" Margin="5">  
                                    <Label Text="{Binding day}" Style="{DynamicResource GreanAndWhite}" FontSize="Large" HorizontalOptions="Center" VerticalOptions="Center"/>  
                                </Frame>  

                                <!--Schedule details-->  
                                <CollectionView x:Name="scheduleCollectionView" Margin="10,10,10,10" FlowDirection="RightToLeft" ItemsSource="{Binding scheduleListByDay, Mode=TwoWay}" >  
                                    <CollectionView.EmptyView>  
                                        <Label  RotationY="{OnPlatform iOS=180}" Text="" Style="{DynamicResource whiteLabel}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />  
                                    </CollectionView.EmptyView>  
                                    <CollectionView.ItemsLayout>  
                                        <LinearItemsLayout Orientation="Vertical" ItemSpacing="6"  />  
                                    </CollectionView.ItemsLayout>  
                                    <CollectionView.ItemTemplate>  
                                        <DataTemplate>  
                                            <Frame  CornerRadius="10" Padding="0"  Style="{DynamicResource ContentFrame}" Margin="10">  
                                                <StackLayout VerticalOptions="Center" Margin="2,0" >  
                                                    <Grid ColumnDefinitions="0.2*,0.4*,0.4*" FlowDirection="RightToLeft" ColumnSpacing="0" >  
                                                        <Grid  Grid.Column="0" RowDefinitions="0.5*,0.5*" Padding="0" HorizontalOptions="Center" VerticalOptions="Center">  
                                                            <Label Text="{Binding STARTTIME}"  Grid.Row="0"   Style="{DynamicResource GreanAndWhite}" />  
                                                            <Label Text="{Binding ENDTIME}" Grid.Row="1"   Style="{DynamicResource GreanAndWhite}" />  
                                                        </Grid>  
                                                        <BoxView BackgroundColor="LightGray" Grid.Column="0" VerticalOptions="Fill" WidthRequest="1"  HorizontalOptions="End"/>  

                                                        <Grid  Grid.Column="1" RowDefinitions="0.5*,0.5*" Padding="0" HorizontalOptions="Center" VerticalOptions="Center" >  
                                                            <Label Grid.Row="0" Text="{Binding COURSE}" Style="{DynamicResource blackLabel}"  />  
                                                            <Label Grid.Row="1" Text="{Binding SECTION}" Style="{DynamicResource blackLabel}" />  
                                                        </Grid>  

                                                        <Button  Grid.Column="2" Text="" Style="{DynamicResource GreenButton}" Margin="10,0,0,0"  VerticalOptions="Center" CornerRadius="10"   Command="{Binding BindingContext.detailsButton,Source={x:Reference ScheduleView}}" CommandParameter="{Binding .}"   />  

                                                    </Grid>  


                                                </StackLayout>  
                                            </Frame>  
                                        </DataTemplate>  
                                    </CollectionView.ItemTemplate>  
                                </CollectionView>  
                            </StackLayout>  
                        </Frame>  
                    </DataTemplate>  
                </CollectionView.ItemTemplate>  
            </CollectionView>  
        </Grid>  

          
    </Grid>  

</ContentPage.Content>  

 

I think the problem is related to many collection views used in my code but I didn't know how to make it better
Are there any best practice tips that are I have to follow to make the performance better

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,369 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.