简体   繁体   中英

Databinding in Xamarin.Forms using a RealObject

Apps running very smoothly but I have decided to add to it. I currently have an APR Calculator but I want users to be able to save APRs. so I currently had my app all in the one class as it was pretty small. The way it was set out was using MVVM. Like I said I now want users to be able to save APRs so I installed RealmDB, I've created a class "APR" and pasted my whole MainWindowViewModel code in to that class I've created. APR class inherits Realm Object I have created an instance of the APR class in MainWindowViewModel but the current bindings in my XAML cannot find my properties... Below is the code for my MainWindowViewModel.

namespace APRooved.ViewModels {

public class MainWindowViewModel : ViewModelBase
{

    APR InstanceOne = new APR();

}

}

I'm sure this is just something silly I'm missing, If someone can point me in the right direction It would be much appreciated.

you can only bind to public properties. InstanceOne is a field, not a property, and not public

public class MainWindowViewModel : ViewModelBase
{
    public APR InstanceOne { get; set; } = new APR();
}

Another way to do this would be initialise your APR class in the constructor

public class MainWindowViewModel : ViewModelBase
{
    public APR InstanceOne { get; set; }

    public MainWindowViewModel(){
        InstanceOne = new APR();
    }
}

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