C
C#17mo ago
Florian Voß

❔ please help fix NullRefException

public partial class MainPage : ContentPage, INotifyPropertyChanged
{
private List<Contact> _contacts;
public List<Contact> Contacts { get => _contacts; set { _contacts = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Contacts")); } }
public event PropertyChangedEventHandler PropertyChanged;
public MainPage()
{
InitializeComponent();
BindingContext = this;
}
private void Button_Clicked(object sender, EventArgs e)
{
Contacts = new List<Contact>
{
new Contact(){ Age = "24" }
};
}
}
public partial class MainPage : ContentPage, INotifyPropertyChanged
{
private List<Contact> _contacts;
public List<Contact> Contacts { get => _contacts; set { _contacts = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Contacts")); } }
public event PropertyChangedEventHandler PropertyChanged;
public MainPage()
{
InitializeComponent();
BindingContext = this;
}
private void Button_Clicked(object sender, EventArgs e)
{
Contacts = new List<Contact>
{
new Contact(){ Age = "24" }
};
}
}
<VerticalStackLayout>
<Button Text="ClickMe" Clicked="Button_Clicked"/>
<ListView ItemsSource="{Binding Contacts}">
<ListView.ItemTemplate>
<DataTemplate>
<Label TextColor="White" BackgroundColor="Green" Text="{Binding Age}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</VerticalStackLayout>
<VerticalStackLayout>
<Button Text="ClickMe" Clicked="Button_Clicked"/>
<ListView ItemsSource="{Binding Contacts}">
<ListView.ItemTemplate>
<DataTemplate>
<Label TextColor="White" BackgroundColor="Green" Text="{Binding Age}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</VerticalStackLayout>
the setter of Contacts property keeps throwing NullRef and I don't know why. As far as I can see I have implemented the interface exactly how they did in the docs https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/binding-mode, but I can't see Contact's Age in my UI.
2 Replies
Florian Voß
Florian VoßOP17mo ago
I found the problem, the <DataTemplate> only allows a child that is a Cell object such as <TextCell>, <ViewCell> ...
Accord
Accord17mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?