简体   繁体   English

使用 RealObject 在 Xamarin.Forms 中进行数据绑定

[英]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.我目前有一个 APR 计算器,但我希望用户能够保存 APR。 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.它的制定方式是使用 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,所以我安装了 RealmDB,我创建了一个“APR”类并将我的整个 MainWindowViewModel 代码粘贴到我创建的那个类中。 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. APR 类继承了 Realm 对象 我在 MainWindowViewModel 中创建了 APR 类的一个实例,但是我的 XAML 中的当前绑定找不到我的属性......下面是我的 MainWindowViewModel 的代码。

namespace APRooved.ViewModels {命名空间 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 InstanceOne是一个字段,不是一个属性,也不是公共的

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另一种方法是在构造函数中初始化您的 APR 类

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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