MVVMCross:绑定android xml中的嵌套属性

时间:2015-08-20 04:19:44

标签: c# xamarin xamarin.android mvvmcross

我有这个ViewModel

public class PersonViewModel
{
    public string Name {get;set}
    public string LastName {get; set;}
    public Location Location {get;set;}
}

和位置对象

public class Location
{
    public decimal Latitude {get;set;}
    public decimal Longitude {get;set;}
    public string Address {get;set;}
}

我希望绑定到PersonViewModel中的Location对象的属性Address,类似这样

<TextView
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    mvx:MvxBind="Text Location.Address"
    android:gravity="center" />

完成简单属性后,一切正常,意味着一切都正确配置,就是这种情况。

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

如前所述Martijn00,上面的代码是正确的。错误的原因是不同的。

我正在使用json文件加载对象。我使用了文件的插件默认位置(data / data / {appnamespace} / files)。

var fileStore = Mvx.Resolve<IMvxFileStore>();
var fileContent = "";
fileStore.TryReadTextFile("Person.json", out fileContent);
JsonConvert.DeserializeObject<PersonViewModel>(fileContent);

android documentation所述,此路由是设备的内部存储器,在卸载应用程序时会被删除。这意味着每次重新部署应用程序时,都会重新创建此路由,删除json文件,从而导致JsonConvert.DeserializeObject抛出异常。