Hi!I am struggling with an MVVM problem as I don't understand why my data is not saved into the sqlite database.I looked over MVVM examples and initially I wanted to use the model class which i reference it in the vm so that i can bind it directly into the view.Unfortunately the values were null.I tried to assign the values of the model to the properties in vm like this:
public Users user {get;set;} public string Name { get{ return user.Name;} set{user.Name = value;} OnNotifyPropertyChanged(); }
This didn't work as it gave me a null error upon initialization of the page regarding the values.As far as I understood you have to create an instance of the vm into the contentpage before doing anything else and that was exactly what I did:
<StackLayout.BindingContext> <l:RegistrationViewModel/> </StackLayout.BindingContext>
Now I am passing the values from the vm to the model into the constructor and the vm values are not null,instead when i try to insert the data,it gives me null values.This is my ViewModel:
` public class RegistrationViewModel : BaseViewModel
{
public int Id { get; set; }
private string name;
private string email;
private string phone;
private string password;
private string confPassword;
RegistrationPage page;
// public ICommand RegisterCommand { get; private set; }
public RegistrationViewModel()
{
}
public RegistrationViewModel(RegistrationPage page)
{
user = new Users();
Id = user.Id;
name = user.Name;
phone = user.Phone;
email = user.Email;
password = user.Password;
confPassword = user.ConfPassword;
this.page = page;
// RegisterCommand = new Command(async () => await Register());
}
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", "Cancel");
if (isUserAccept)
{
App.Data.AddUser(user);
await page.DisplayAlert("Registration Succesful", "You have been registered" + "" + name, "OK");
}
/* if (user == null)
{
await page.DisplayAlert("Registration went wrong", "Please complete all fields", "OK");
}*/
}
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged("Name");
}
}
public Users user { get; set; }
public string Email
{
get { return email; }
set
{
email = value;
OnPropertyChanged("Email");
}
}
public string Phone
{
get { return phone; }
set
{
phone = value;
OnPropertyChanged("Phone");
}
}
public string Password
{
get { return password; }
set
{
password = value;
OnPropertyChanged("Password");
}
}
public string ConfPassword
{
get { return confPassword; }
set
{
confPassword = value;
OnPropertyChanged("ConfPassword");
}
}
}`
The View:
`<?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">
<ContentPage.BindingContext>
<l:RegistrationViewModel/>
</ContentPage.BindingContext>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="20,0">
<Entry IsPassword="False" x:Name="userName" Placeholder="Name" Text="{Binding Name, Mode=TwoWay}" PlaceholderColor="Black" HeightRequest="40"/>
<Entry IsPassword="False" x:Name="userEmail" Placeholder="Email" Text="{Binding Email, Mode=TwoWay}" PlaceholderColor="Black" HeightRequest="40"/>
<Entry IsPassword="False" x:Name="userPhone" Placeholder="Phone" Text="{Binding Phone, Mode=TwoWay }" PlaceholderColor="Black" HeightRequest="40"/>
<Entry IsPassword="True" x:Name="userPassword" Placeholder="Password" Text="{Binding Password, Mode=TwoWay}" PlaceholderColor="Black" HeightRequest="40"/>
<Entry IsPassword="True" x:Name="userConfPassword" Placeholder="Confirm Password" Text="{Binding ConfPassword, Mode=TwoWay}" PlaceholderColor="Black" HeightRequest="40"/>
<Button x:Name="registerUser" Command="{Binding RegisterCommand,Mode=TwoWay}" Text="Register"/>
</StackLayout>
</ContentPage>`
The Model:
` [Table("Users")]
public class Users
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
[NotNull]
public string Name { get; set; }
[NotNull]
public string Phone { get; set; }
[NotNull]
public string Password { get; set; }
[NotNull]
public string ConfPassword { get; set; }
[NotNull]
public string Email { get; set; }
public Users() { }
}`
Can someone help me understand what am I doing wrong?Best regards!