microsoft / Appsample-Photosharing

Sample code for a UWP photo sharing app.
Other
286 stars 116 forks source link

Setting the DataContext AFTER InitializeComponents causes errors #40

Open ToddBonnewell opened 7 years ago

ToddBonnewell commented 7 years ago

In general the pattern in this project sets the DataContext to the ViewModel AFTER InitializeComponents is called. When this happens, the xaml is looking to bind to properties that do not yet exist in the ViewModel. Retrieving the ViewModel from the ServiceLocator should happen BEFORE in order to make sure the dependencies exist. Errors appear in the Diagnostics Tools otherwise.

This issue is not apparent because OnNavigatedTo then assigns the DataContext to the ViewModel and the bindings are updated.