question

EduardoGomez-1870 avatar image
0 Votes"
EduardoGomez-1870 asked JarvanZhang-MSFT edited

My Sqlite inset, but whe I read it there is none

 public class Contact : ViewModelBase {

     [PrimaryKey, AutoIncrement]
     public int Id { get; set; }

     private string _Name;
     public string Name {
         get { return _Name; }
         set {
             if (_Name != value) {
                 _Name = value;
                 RaisePropertyChanged();
             }
         }
     }

     private string _PhoneNumber;
     public string PhoneNumber {
         get { return _PhoneNumber; }
         set {
             if (_PhoneNumber != value) {
                 _PhoneNumber = value;
                 RaisePropertyChanged();
             }
         }
     }

     private string _Address;
     public string Address {
         get { return _Address; }
         set {
             if (_Address != value) {
                 _Address = value;
                 RaisePropertyChanged();
             }
         }
     }
 }

}

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace ContactXam.ViewModel {
public class ViewModelBase : INotifyPropertyChanged {

     #region Notify Property Changed Members
     public event PropertyChangedEventHandler PropertyChanged;
     protected void RaisePropertyChanged([CallerMemberName] string propertyName = "") {
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
     }
     #endregion
 }

}

dotnet-xamarinformsdotnet-sqlite
· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My SQLite insert, but when I put a for, to retrieve the data, there's 0

Hi, EduardoGomez-1870. Try to add a breakpoint to check if the 'contacts' is empty. If it's empty, there is something wrong with inserting the data. If not, there may be something wrong with the UI updating. Please make sure the PropertyChanged event is triggered when the data is updated.

0 Votes 0 ·

I found the problem my properties are null, and I can't understand why

I will update the code

0 Votes 0 ·

My Contact adress, number and name are null

0 Votes 0 ·
jcmanke avatar image jcmanke EduardoGomez-1870 ·

That's because you're creating a new Contact inside of the save command. Instead of binding from the UI directly to your database model, use the ViewModel. Put the properties on the VM and bind the Entries to them. In the save command, create the Contact and set its properties to the values from the ViewModel.

1 Vote 1 ·
Show more comments
JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

when I save a contact and come POP the page, I don't see my new contact, I have to restart the app to see

To update the UI at runtime, please make sure the model class inherits from the INotifyPropertyChanged interface or the BindableObject class to raise the PropertyChanged event such as:

public class TestModel : INotifyPropertyChanged
{
    private string content;
    public string Content
    {
        get
        {
            return content;
        }
        set
        {
            if (content != value)
            {
                content = value;
                NotifyPropertyChanged("Content");
            }
        }
    }

    protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

Check the doc: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I did, that class inherits my viewModelBase, which has implemented the PropertyChanged, let me Update my code

0 Votes 0 ·

Hi,duardoGomez-1870. It requires the properties of the model class also raise the PropertyChanged event. Please make sure the model class implement the INotifyPropertyChanged interface.

0 Votes 0 ·

they implement it

0 Votes 0 ·
sphauck avatar image
0 Votes"
sphauck answered

Here is a simple example of my code to insert 1 or many of something

78505-image.png



image.png (45.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.