Error in the Xamarin MainActivity class when using Grid layout control as Listview Item Template.

CKuttan 21 Reputation points
2020-11-21T14:59:44.88+00:00

I was working with a wpf listview and when running the application, it throws an error - System.InvalidCastException: 'Specified cast is not valid.' The error is thrown at LoadApplication(new App()) in the MainActivity class. This error is throwing because I tried to use Grid control in the Listview itemtemplate. Here is the XAML part (wpf project) which causes the error.

       <ListView  x:Name="lstView" ItemsSource="{Binding Foods}" RowHeight="80">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackLayout Orientation="Vertical">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="30"/>
                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <Image Source="{Binding FoodImage}" Grid.Column="0" Grid.RowSpan="3" HorizontalOptions="End" />
                        <Label Text = "{Binding FoodName}" Grid.Column="1" Grid.Row="0" FontSize="12" />
                        <Label Text = "{Binding Price}" Grid.Column="1" Grid.Row="1" FontSize="12" />
                        <Entry Text="{Binding Quantity}" Grid.Column="1" Grid.Row="2" FontSize="12" />
                    </Grid>
                </StackLayout>                   
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

If i use a ImageCell in the place of Grid, it will work properly. But I need to have a textbox in each row beside the image. that's why I used Grid to have a textbox in each listviewitem.

Kindly show a way.

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

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,961 Reputation points
    2020-12-17T02:32:02.587+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Hi, @CKuttan . This is because cell is required in ListView, try using a cell to wrap the stackLayout. Check the code:

       <ListView  x:Name="lstView" RowHeight="80" ...>  
           <ListView.ItemTemplate>  
               <DataTemplate>  
                   <ViewCell>  
                       <StackLayout Orientation="Vertical">  
                           ...  
                       </StackLayout>  
                   </ViewCell>  
               </DataTemplate>  
           </ListView.ItemTemplate>  
       </ListView>  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

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.