Checking the launch of the application, where should I execute them in the MVVM template?

I would like to run a C # / WPF / MVVM application with an instance of my main view model after checking the local file for the database connection settings.

As soon as I have the connection settings, I will bind them to the class and include them in my main view model.

I think I would check the local file and instantiate the Main View Model and Main Window in App.xaml.cs

Any thoughts? Is there a better way to do this?

+4
source share
2 answers

I usually do what you said: create an initial View and ViewModel in App.xaml.cs

 protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var vm = new MainViewModel(); // set vm properties var view = new MainView(); view.DataContext = vm; view.Show(); } 
+3
source

You can implement this responsibility in a separate class that is responsible for navigation (loading (ViewModel and) viewing and listening to navigation messages).

In the App class, you just need to instantiate and initialize this class.

+1
source

Source: https://habr.com/ru/post/1391180/


All Articles