I have a question: Where is the best place to create the ViewModel in MVVM and how?
1) Create once in App.xaml.cs as static field and then use it through App?
2) Alway create new ViewModel in Page.cs, when I navigate to this page?
3) other options
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In MVVM, the ViewModel is the application. This means that I usually have an single startup ViewModel that is the entry point to my application, and I typically create an instance of this in the App.xaml.cs
OnStartupcodeEvery once in a while I have an application that will create the ViewModel in the startup window’s constructor, but this is not really preferred because it means if I have any startup logic, I have to put that in the code-behind the View as well, and I don’t like mixing application logic in my View layer.
Regardless of how you do it, keep in mind that when using MVVM, your ViewModels are your application, not your Views, so typically your ViewModels are connected in some way to the startup ViewModel. Views are just a user-friendly way for users to interact with your application (the ViewModels).