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?