【问题标题】:MVVMCross: Bind nested properties in android xmlMVVMCross:在 android xml 中绑定嵌套属性
【发布时间】:2015-08-20 04:19:44
【问题描述】:

假设我有这个 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" />

使用简单属性后一切正常,这意味着一切都已正确配置,仅适用于这种情况。

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

【问题讨论】:

  • 这似乎完全正确。我使用相同的结构来做一些数据绑定。
  • PersonViewModel 是否继承自 MvxViewModel ?
  • 正如您之前提到的,所描述的代码工作正常。经过一些调试,我发现我用来加载对象的 json 有时会被删除。要读取我正在使用 IMvxFileStore.TryReadTextFile() 的文件,该文件位于 data/data/{appnamespace}/files 中。现在我需要检查为什么会发生这种情况

标签: c# xamarin xamarin.android mvvmcross


【解决方案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 抛出异常。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-05-05
  • 2014-12-16
  • 1970-01-01
  • 2012-07-19
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多