I am having some problems understanding xaml with mvvm. Sometimes works but others doesn't.
ViewModel (implements INotifyPropertyChanged):
private Class1 firstClass;
public Class1 FirstClass{
get{return firstClass;}
set{firstClass = value; OnPropertyChanged();}
}
private string name;
public string Name{
get{return name;}
set{name = value; OnPropertyChanged();}
}
private string address;
public string Address{
get{return address;}
set{address = value; OnPropertyChanged();}
}
View:
private ViewModel vm;
In the View constructor:
vm = new ViewModel(id);
BindingContext = vm;
OnAppearing (async):
base.OnAppearing();
await vm.LoadDataAsync();
lAddress.SetBinding(Label.TextProperty, new Binding("Address");
If I set the BindingContext in xaml and remove it from the constructor, it does not work.
If I set the Binding on the Address label in xaml and remove it from the code behind, it does not work.
If I try to use Name as the Title of the page, it does not work.
In any of these cases I am not getting any error like 'Binding: property not found on BindingContext' so I understand that they are being found but maybe they are empty.
If I modify a property from Class1, it does not appear on the page. Can I assume that the reason is that Class does not implement INotifyPropertyChanged?
Is it better or advisable to LoadData in VM constructor (Task.Run) or on Page.OnAppearing(await vm.LoadData())?
Could anybody provide a bit of guidance?
EDIT:
This is getting to a fair bit of debugging (and a reason why one question per post might make more sense); but here goes:
Your view model has a constructor that takes a parameter. If you construct in XAML it will use the default. Basically, the two pieces of code you are comparing are not equivalent. If you need the parameter keep it like you have it.
You'll need to debug this one; ensure the bound property isn't actually empty for starters.
No idea; same as the address you probably need to debug it.
Your interpretation of this is correct; I would be checking that my data is loaded properly.
Yes. Your current code will only update if FirstClass
is reassigned. To catch assignment of properties within that object Class1
will need INPC (perhaps this explains 2 and/or 3)
Not sure it really matters.