question

CKuttan-1986 avatar image
0 Votes"
CKuttan-1986 asked JarvanZhang-MSFT answered

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

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.



dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

Hi, @CKuttan-1986. 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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.