Search code examples
c#xamarinxamarin.androidmvvmcross

MVVMCross: Bind nested properties in android xml


Supose i have this ViewModel

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

and the Location object

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

and I want to bind to the property Address of the Location object inside PersonViewModel, something like this

<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" />

When done with simple properties everything works fine, meaning everything is correctly configure, is just with this case.

How is the correct way to do this ?


Solution

  • As previously mentioned by Martijn00, the code above is correct. The cause of the error was different.

    I'm using a json file to load the objects. I used the file's plugin default location (data/data/{appnamespace}/files).

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

    As described by the android documentation, this route is the device's internal memory and is deleted when the application is uninstalled. This means that every time the applications is re-deployed, this route is re-created deleting the json file and hence causing the JsonConvert.DeserializeObject to throw an exception.