Hi!I have a project in Xamarin.Forms and I am using MVVM.The problem is that when I start the android emulator and the page is initializing,I get an error System.Reflection.TargetInvocation where I set the BindingContext in the view:
RegistrationPage.cs
public partial class RegistrationPage : ContentPage
{
public RegistrationPage(RegistrationViewModel vm)
{
InitializeComponent();
this.BindingContext = vm;
}
}
When I click the error to see the details,it shows System.NullReferenceException on the values from my ViewModel which I bind in the View saying:"Object reference not set to an instance of an object".I believe that it should be normal for them to be null,but I don't understand why I get this error since the page won't even load,it will just break and show me the error.This is the rest of the code:
RegistrationPage.xaml
<pre><?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:l="clr-namespace:Edu.VM"
mc:Ignorable="d"
x:Class="Edu.Views.RegistrationPage">
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="20,0">
<Entry IsPassword="False" x:Name="userName" Placeholder="Name" Text="{Binding Name}" PlaceholderColor="Black" HeightRequest="40"/>
<Entry IsPassword="False" x:Name="userEmail" Placeholder="Email" Text="{Binding Email}" PlaceholderColor="Black" HeightRequest="40"/>
<Entry IsPassword="False" x:Name="userPhone" Placeholder="Phone" Text="{Binding Phone }" PlaceholderColor="Black" HeightRequest="40"/>
<Entry IsPassword="True" x:Name="userPassword" Placeholder="Password" Text="{Binding Password}" PlaceholderColor="Black" HeightRequest="40"/>
<Entry IsPassword="True" x:Name="userConfPassword" Placeholder="Confirm Password" Text="{Binding ConfPassword}" PlaceholderColor="Black" HeightRequest="40"/>
<Button x:Name="registerUser" Command="{Binding RegisterCommand}" Text="Register"/>
</StackLayout>
</ContentPage>
RegistrationViewModel.cs
public class RegistrationViewModel : BaseViewModel
{
private Users user;
RegistrationPage page;
public RegistrationViewModel()
{
}
public RegistrationViewModel(RegistrationPage page,Users s)
{
user = s;
this.page = page;
}
public ICommand RegisterCommand => new Command(async () => await Register());
public async Task Register()
{
bool isUserAccept = await Application.Current.MainPage.DisplayAlert("Add contact", "Do you want to register?", "", "OK");
if (isUserAccept)
{
await App.Data.AddUser(user);
await page.DisplayAlert("Registration Succesful", "You have been registered" + "" + user.Name, "OK");
}
if (user == null)
{
await page.DisplayAlert("Registration went wrong", "Please complete all fields", "OK");
}
}
public string Name
{
get { return user.Name; }
set
{
user.Name = value;
OnPropertyChanged("Name");
}
}
public string Email
{
get { return user.Email; }
set
{
user.Email = value;
OnPropertyChanged("Email");
}
}
public string Phone
{
get { return user.Phone; }
set
{
user.Phone = value;
OnPropertyChanged("Phone");
}
}
public string Password
{
get { return user.Password; }
set
{
user.Password = value;
OnPropertyChanged("Password");
}
}
public string ConfPassword
{
get { return user.ConfPassword; }
set
{
user.ConfPassword = value;
OnPropertyChanged("ConfPassword");
}
}
}
Did anyone encounter this kind of problem before?Any help would be appreciated.
What I have tried:
I have referenced the model class in the ViewModel and assigned the values of the model to the viewmodel in the constructor:
Name = user.Name;
Email = user.Email;
Password = user.Password;
ConfPassword = user.ConfPassword;
Phone = user.Phone;
I set the binding context directly into the view:
<StackLayout.BindingContext>
<l:RegistrationViewModel>
</StackLayout.BindingContext>
I deleted the TwoWay binding property for the values in the view and I also tried to check for null values in the code behind like this:
if(string.IsNullOrEmpty(name.Text)){
InitializeComponent();
}