0

I am running into a scenario where I am binding to a property from my viewmodel in my xaml view where the VM property might be null. This causes my view not to load because I believe that I am getting a NullReferenceException.

VM:

 public class PersonDetailViewModel : ViewModelBase
    {
        public Person CurrentPerson
        {
            get => currentPerson;
            set => SetProperty(ref currentPerson, value);
        }

        private Person currentPerson;

        public bool IsBobsFriendsVisible => FriendNamedBob?.Friends?.Count > 0;
        public Person FriendNamedBob => CurrentPerson?.Friends?.FirstOrDefault(x => x.Name == "Bob");

        public PersonDetailViewModel()
        {
            CurrentPerson = new Person()
            {
                Name = "Henry",
                Friends = new List<Person>() { new Person() { Name = "Rachel" } }
            };
        }
    }

XAML:

<ContentPage>
    <ContantPage.Content>
        <StackLayout>
            <Label Text="Bob's Friends Count:" IsVisible="{Binding IsBobsFriendsVisible}" />
            <Label Text="{Binding FriendNamedBob.Friends.Count}" IsVisible="{Binding IsBobsFriendsVisible}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

This line is causing the issue obviously since FriendNamedBob is null:

<Label Text="{Binding FriendNamedBob.Friends.Count}" IsVisible="{Binding IsBobsFriendsVisible}" />

What is the recommended technique for dealing with this scenario? Is this a sign of bad design?


Update: It seems that a FallBackValue of sorts (TargetNullValue included) is not yet supported in Xamarin.Forms https://github.com/xamarin/Xamarin.Forms/issues/1803 also the DataTrigger does not work when checking for null https://bugzilla.xamarin.com/show_bug.cgi?id=57863

Thus, this is not a duplicate (yet) -- what are people currently doing in this situation?

user2072256
  • 153
  • 7
  • How can I "unduplicate" this question? I researched the suggestion and I have good reason to believe it is not a duplicate - does Stackoverflow really force me to delete and re-add the question? @BradleyDotNET – user2072256 Jul 25 '18 at 01:14
  • flag and ask a moderator to re-open – Jason Jul 25 '18 at 01:52
  • @Jason I did. Very confused. – user2072256 Jul 25 '18 at 01:57
  • 1
    you should be good now – Jason Jul 25 '18 at 01:58
  • I'd suggest that you create a property in your VM that handles the null case correctly and bind to it, instead of using the complex binding expression that you have – Jason Jul 25 '18 at 02:00
  • @Jason true... Just would require a lot of properties in my actual scenario however perhaps that isn't the worst thing in the world given that it would solve the problem. Was hoping for an "easier" way. – user2072256 Jul 25 '18 at 02:51

1 Answers1

0

Here let me explain to handle the Null binding in Xaml. And i used same format to bind the list of object in xaml and it's work.

Public Class Person
{
  Public Person()
  {
  }
      int id=0;--- Here we are doing default Initialization of object to handle default null values 
        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
                OnPropertyChanged();
            }
        }

        string name="";--- Here we are doing default Initialization of object to handle default null values 
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                OnPropertyChanged();
            }
        }
}

Now we can bind directly in XAML

Pratius Dubey
  • 673
  • 6
  • 19