简体   繁体   中英

Binding: Property not found. MVVM

I am trying to bind properties of views in the view page to a class that I have in a carpet called ViewModel, and then from an instance of another class called calculator (a model) in a carpet called Model I am trying to access to properties contained there, the problem is that it does not seem to work; in the output section, I get the following message: Binding: 'N2' property not found on 'XamForms.ViewModel.MainPageViewModel' . Where N2 is a property of the Model class "calculator". I will explain it with more details in the code:

MainPage.xaml code:

<Entry
    x:Name="n1"
    Text="{Binding calculator.N1}"
></Entry>
<Entry
    x:Name="n2"
    Text="{Binding calculator.N2}"
></Entry>
<Button
    BackgroundColor="LimeGreen"
    Command="{Binding Operations}"
></Button>

The binding to Operations works, since it is in the ViewModelPage and not in the calculator (model) one, as you will see.

MainPage.xaml.cs code:

public MainPage()
{
    MainPageViewModel mainPageViewModel = new MainPageViewModel();
    this.BindingContext = mainPageViewModel;
}

MainPageViewModel code:

class MainPageViewModel
{
    public Command Operations { get; set; }
    public Calculator calculator;
    public MainPageViewModel()
    {
        Operations = new Command(DoOperations);
        calculator = new Calculator();
    }

    private void DoOperations()
    {
        calculator.Division = calculator.N1 / calculator.N2;
        //Here is where I get the message, N1 and N2 are null, but they should have the values that I 
        //inserted on the entry, the binding to Division is also incorrect.
    }
}

Calculator (Model) code:

class Calculator : INotifyPropertyChanged
{

private decimal n1;
public decimal N1
{
    get
    {
        return n1;
    }
    set
    {
        n1 = Convert.ToDecimal(value);
    }
}

private decimal n2;
public decimal N2
{
    get
    {
        return n2;
    }
    set
    {
        n2 = Convert.ToDecimal(value);
    }
}

private decimal division;
public decimal Division
{
    get
    {
        return division;
    }
    set
    {
        division= Convert.ToDecimal(value);
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

I am studying Xamarin Forms and MVVM so it is probably an easy mistake, but I can't find it and all the related solutions that I have found are too complex for my actual level so I can't extrapolate them. If you need more information I will give it as soon as I see it, thanks for your time, have a good day.

the binding of Operations works, because it is declared as property (with getter and setter): public Command Operations { get; set; } public Command Operations { get; set; }

public Calculator calculator; is a simple field . Bindings don't support fields. Make it a property :

public Calculator calculator { get; set; }

In Model 'Calculator' for properties N1 and N2 you need to call "OnPropertyChanged" in set part of property.

Ex:

private decimal n2;
public decimal N2
{
   get
   {
       return n2;
   }
   set
   {
       n2 = Convert.ToDecimal(value);
       OnPropertyChanged(n2);
   }
}

You need to make Calculator class as 'public'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM