I want to set a dirty flag for any of the required properties in my view model. I initialize IsDirty
to false in the constructor. Unfortunately all of the setters in my properties are called after the constructor. Is there a way I can set IsDirty
to false after all of the setters? The setters all have a line IsDirty=true
;
I'm using the Prism framework with Xamarin 4.0, but Prism documentation doesn't have anything on the ViewModel life cycle.
My redacted constructor looks like this:
public SomeDetailsViewModel(INavigationService navigationService) : base(navigationService)
{
Sample = new SampleDTO();
InitializeLookupValues();
_samplesService = new SampleService(BaseUrl);
TextChangedCommand = new Command(() => OnTextChanged());
AddSampleCommand = new Command(() => AddCurrentSample());
CancelCommand = new Command(() => Cancel());
IsDirty = false;
}
Edit 3:
The constructor calls InitializeLookupValues()
. These appear to be the culprit.
private async Task InitializeLookupValues()
{
App app = Prism.PrismApplicationBase.Current as App;
string baseUrl = app.Properties["ApiBaseAddress"] as string;
_lookupService = new LookupDataService(baseUrl);
int TbId = app.CurrentProtocol.TbId;
int accessionId = CollectionModel.Instance.Accession.AccessionId;
Parts = await _lookupService.GetParts(accessionId);//HACK
Containers = await _lookupService.GetSampleContainers(TbId);
Additives = await _lookupService.GetAdditives(TbId);
UnitsOfMeasure = await _lookupService.GetUnitsOfMeasure();
// with a few more awaits not included.
}
After exiting the constructor each of the properties are set. They look like this one.
public ObservableCollection<PartDTO> Parts
{
get
{
return parts;
}
set
{
SetProperty(ref parts, value);
}
}
private PartDTO part;
public PartDTO SelectedPart
{
get
{
return part;
}
set
{
SetProperty(ref part, value);
IsDirty = true;
}
}
Where IsDirty is defined thus:
private bool isDirty;
public bool IsDirty
{
get
{
return isDirty;
}
set
{
SetProperty(ref isDirty, value);
Sample.DirtyFlag = value;
}
}
I haven't explicitly set any of the properties. I would like to avoid their being initialized automatically, or call something after them.
Edit
Just a note to everyone I have been debugging to find out what I could. I found that in each data-bound property the getter is called twice, then the setter is called. I looked at what generated code I could find, and there is no obvious place where data binding is explicitly calling the setter.
Edit 2
What I hadn't shown before, and now looks likes it's a critical piece of information, was that I populate the ObservableCollection
with an async call to a service. As far as I can tell, because of XAML data binding, the SelectedPart
property setter is called. If I debug slowly this start to show in some places. I've added the async call above.