Skip to content

How to inject viewmodel into page #1540

Answered by nickrandolph
HakanL asked this question in Q&A
Discussion options

You must be logged in to vote

Pages and Controls shouldn't take parameters in the constructors. In theory this rule could be broken for non-Page controls, since they could be instantiated from DI before being added to the visual tree. However, this will break hot reload scenarios in the future, so best to stick with the no-parameters in constructor rule.
The issue with Pages is that they get instantiated by a Frame as part of Frame.Navigate. This is somewhat painful as it means the navigation code doesn't get an opportunity to set the datacontext before the Page is navigated to

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
2 replies
@nickrandolph
Comment options

@HakanL
Comment options

Comment options

You must be logged in to vote
3 replies
@HakanL
Comment options

@nickrandolph
Comment options

@HakanL
Comment options

Answer selected by HakanL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants