skylerWithAnE
skylerWithAnE

Reputation: 102

What is Causing this Exception in Xamarin xaml?

I've been working on a UWP app for quite some time. We recently decided to transition over to Xamarin so that we can easily support Android alongside UWP. I've had no issues setting up a PCL to use for our code base. Now I'm running into some nightmares when trying to rewrite our UI for Xamarin.Forms. I'm trying to accomplish something relatively simple and I must be doing something hilariously wrong, but, I cannot figure out what.

I want to display a listView. Here's the XAML:

<ContentPage.Content>
    <StackLayout>
        <Label Text="This is the bill page."
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
        <ListView x:Name="lvBills" 
                  ItemsSource="{Binding Bills}" 
                  SelectedItem="{Binding SelectedBill}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Horizontal">
                        <Label Text="{Binding Amount}"
                               TextColor="White"
                               VerticalOptions="CenterAndExpand" 
                               HorizontalOptions="CenterAndExpand"/>
                        <Label Text="{Binding DueDate}"
                               VerticalOptions="CenterAndExpand" 
                               HorizontalOptions="CenterAndExpand"
                               TextColor="White"/>
                    </StackLayout>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

When I run it, the listView populates with each item in my observable collection. However, no text is actually displayed..even if I remove the bindings and use some placeholder text. Additionally, if I click on any item in the listView, this exception crashes the program.

System.InvalidCastException: Unable to cast object of type 
'Xamarin.Forms.StackLayout' to type 'Xamarin.Forms.Cell'.
at Xamarin.Forms.Internals.TemplatedItemsList`2.ActivateContent(Int32 index, 
Object item)
at Xamarin.Forms.Internals.TemplatedItemsList`2.CreateContent(Int32 index, O 
Object item, Boolean insert)
at Xamarin.Forms.Internals.TemplatedItemsList`2.GetOrCreateContent(Int32 
index, Object item)
at Xamarin.Forms.Internals.TemplatedItemsList`2.get_Item(Int32 index)
at Xamarin.Forms.L

I can't figure out any search that yields any meaningful results. I've tried some other things: Adding a binding for ItemSelected in the xaml. I've tried to wire up delegates in the xaml.cs But I cannot make any progress. So, what am I doing wrong?

Upvotes: 2

Views: 1027

Answers (1)

DavidG
DavidG

Reputation: 118937

You need a ViewCell inside a DataTemplate:

<ContentPage.Content>
    <StackLayout>
        <Label Text="This is the bill page."
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
        <ListView x:Name="lvBills" 
                  ItemsSource="{Binding Bills}" 
                  SelectedItem="{Binding SelectedBill}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell> <!-- <----- Add this -->
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Amount}"
                                   TextColor="White"
                                   VerticalOptions="CenterAndExpand" 
                                   HorizontalOptions="CenterAndExpand"/>
                            <Label Text="{Binding DueDate}"
                                   VerticalOptions="CenterAndExpand" 
                                   HorizontalOptions="CenterAndExpand"
                                   TextColor="White"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

Upvotes: 8

Related Questions