0

I'm trying to create a custom cell but when I run the app and navigate to the page where its ListView is supposed to be, I'm getting a NullReferenceException with no further details to go by. I might have done something wrong too because documentation for creating custom cells using XAML is quite lacking. Besides I'm new to Xamarin.

Here's what I tried to do:

TestViewCell.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MovieDbApp.UI.TestViewCell">
    <ViewCell.View>
        <StackLayout>
                <Label Text="{Binding Title}"/>
        </StackLayout>
    </ViewCell.View>
</ViewCell>

TestViewCell.xaml.cs (Not sure this is right...)

public partial class TestViewCell : ViewCell
{
    public static readonly BindableProperty TitleProperty =
        BindableProperty.Create("Title", typeof(string), typeof(TestViewCell ),"");

    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
}

TestViewCellPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MovieDbApp.UI"
             x:Class="MovieDbApp.View.TestViewCellPage">
    <ContentPage.Content>
        <StackLayout>
            <ListView x:Name="listView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <local:TestViewCell Title="{Binding Title}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Intellisense will auto-complete the property "Title" for me in TestViewCellPage.xaml, but the application throws a NullReferenceException during runtime. What am I missing here?

makoshichi
  • 2,310
  • 6
  • 25
  • 52
  • 2
    instead of building a custom cell, I would just put your ViewCell directly in your data template, get it working, and then refactor into a custom cell. – Jason Aug 15 '18 at 04:26
  • 2
    Yes.. also we dont know whats wrong, because you haven't shown the line or the code thats throwing it – TheGeneral Aug 15 '18 at 04:26
  • Sorry guys, I overlooked something else and thought the problem lied within the View Cell, which is not the case. The exception location wasn't being shown on VS, but I used the call stack to track it down. Thanks for your time – makoshichi Aug 15 '18 at 11:45

0 Answers0