Creating a ViewModel: Do this before or after model data is available?

I have a WPF application that loads data from a data source.

While some data is not loaded, nothing is visible.

My question is me:

  • Create a V and VM before any data is available; set the data in the virtual machine after exiting
  • Create only V at the beginning; wait until data is available, then create a virtual machine by entering data
  • Create both V and virtual machine only after data is available
+3
source share
4 answers

I say Create both V and VM before any data is available; set the data in the virtual machine when available.

Show the IsLoading indicator before loading data and loading data into a separate stream. Otherwise, if the data takes time to download, you will get a terrible wpf black screen.

It’s better if the view is loaded immediately, even if you need to wait for the data to load, it is perceived as faster.

+8
source

What I do is create my virtual machine and browse right away and display the view.

As in many applications, most of my model takes time to launch, with the exception of the list of startup status, which is available immediately, is constantly updated during startup, sent to the virtual machine, and then to the progress bar in my View.

My virtual machine joins events in the Model that click on the virtual machine as they occur. In the View XAML I view for VM for each VM property.

Thus, the user receives information.

+4
source

I accepted what Josh Smith did in his MSDN article here ... Scroll down to the part, he talks about applying View in the ViewModel. In this case, the View is created during the rendering of the ViewModel. There is no need to manually create a view and then assign a DataContext to the ViewModel. It does this automatically for you.

"You can easily tell WPF how to render a ViewModel using typed DataTemplates. A printed DataTemplate does not have an x: Key value assigned to it, but it does have its DataType property set by an instance of the Type class. If WPF tries to display one of your ViewModel objects, it checks if the resource system has a typed DataTemplate in an area whose DataType is the same (or base class) type of your ViewModel. If it finds one, it uses this template to render the ViewModel referenced by the content property of the tab. "

In other words, you should create your ViewModel as follows:

MyViewModel viewModel = new MyViewModel(); // Add the view model to the content of some control (TabItem, Grid, Window, etc.) // NOTE: You wouldn't actually make this call... instead you would add the // ViewModel to a collection or a property and the parent would bind // to it and display it properly MyContainer.Content = viewModel; 

And in your ResourceDictionary you define this:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:DemoApp.ViewModel" xmlns:vw="clr-namespace:DemoApp.View" > <!-- NOTE: The View must be a UserControl (or page) --> <DataTemplate DataType="{x:Type vm:MyViewModel}"> <vw:MyView /> </DataTemplate> </ResourceDictionary> 
+2
source

Creating only a View without a viewModel is a pointless IMO - what about all the related properties from a View? They have nothing to bind since the ViewModel does not exist. The fact that you do not see any exceptions does not mean that everything is in order :)

Of course, you must initiate your ViewModels for all views (of course, the Views currently in use :)) at the very beginning - this is not only recommended, but also required.

+1
source

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


All Articles