NullReferenceException thrown in NotifyPropertyChanged when switching views with edit in progress
I have a series of views that are to be displayed depending on the currently selected item in a tree within a parent view. These views are created and registered with the region during the initialization method of the par开发者_如何学运维ent view and are being correctly deactivated/activated, therefore giving the effect of swapping in/out the correct view. These views have a single underlying viewmodel as their datacontext, which contains data objects supporting INotifyPropertyChanged.
This solution works if there are no currently outstanding edits in progress within the child view but if there is a edit in progress in a view (i.e. the user has changed the contents of a description but hasn't clicked out of the text box) and that view is deactivated (i.e. the a different tree item is clicked within the parent view, thus causing a de-activation to occur) a NullReferenceException is being thrown in the NotifyPropertyChanged() of the underlying data object attached to the now deactivated view.
What seems to be happening is this:
- An edit is started by the user in the child view
- The user clicks an item in the tree in the parent view
- The controller picks up the change in the selected item in the tree
- The current child view is deactivated
- The new view is activated
- The change from the edit happens to the underlying data object (the set method is getting called)
- A change notification event is generated by the data object as a result of this change
- A null reference exception is thrown.
Presumably, this change notification event is being sent to the now de-activated view, but the view is not null.
I have not tried this myself, but I believe one solution was to listen for the deactivate event of the view using IActiveAware
and cancel any editing.
See if this link helps.
精彩评论