I'm new to Xamarin Forms and trying to bind a nullable property in XAML :
public DateTime? LocalExpiryDate //property on Person class
{
get { return GetValue<DateTime>(); }
set { SetValue(value); }
}
<Label Text="{Binding LocalExpiryDate}"/>
And binding in the code behind as:
InitializeComponent();
MainStackLayout.BindingContext = member.Person;
Even though the property isn't null when I try and run the app on the Android Emulator I get an error "Exception has been thrown by the target of an invocation", and drilling down into the error I get a message "Object reference not set to an instance of an object"
If I remove the ? to make the property not nullable then the app works fine as long as there is a property
Is it not possible to bind to a nullable property or is there a way around this?
Thanks
Mark
As I understand your issue. I have Page
:
public partial class Page1 : ContentPage
{
private Person _person;
public Person Person
{
get { return _person; }
set
{
_person = value;
OnPropertyChanged();
}
}
public Page1()
{
BindingContext = this;
InitializeComponent();
}
protected override void OnAppearing()
{
Person = new Person() { Date = DateTime.Now };
base.OnAppearing();
}
}
I have Person
:
public class Person : BindableObject
{
private DateTime? _date;
public DateTime? Date
{
get { return _date; }
set
{
_date = value;
OnPropertyChanged();
}
}
}
And I have XAML
:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CommonSample;assembly=CommonSample"
x:Class="CommonSample.Page1">
<StackLayout>
<Label Text="{Binding Person.Date}"/>
</StackLayout>
</ContentPage>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments