问题描述:

I am new to MVVM and I'm hoping somebody can clarify a couple things for me. I need to have a label that displays a date in my main view.

I made a model class "Date":

public class Date

{

DateTime _date;

public DateTime Date { get { return _date; } }

}

Do I need to make a ViewModel for Date and then pass that in to MainViewModel? Could I use Date directly in MainViewModel or would that be a bad idea?

If this is the case, does every model need its own ViewModel even if it will only be used in the main ViewModel? Or is there another way to go about this? Does "Date" even need to be its own model?

Any advice or tips would be greatly appreciated. Sorry for the torrent of questions; my goal is to learn how to implement MVVM properly and efficiently.

网友答案:

You don't need a viewModel per class at all. Just implement InotifyPropertyChanged properly and set your model instance in your mainviewModel.

Date doesn't need it's own model, just set it in a property that implement the propertyChanged event.

private DateTime _Date;
public DateTime Date
{
   get { return _Date; }
   set 
   { 
      if (value != _Date)
      {
         _Date = value;
         RaisePropertyChanged(() => Date);
      }
   }
} 
网友答案:

You don't need 1 to 1 relationship between Models and ViewModels. The ViewModel's purpose is to expose the things needed to be bound to the View in the way the View needs them. That can be simple properties of built in types like bool and DateTime or they can be complicated structures (like types in your own Model).

I would say that Date already is its own model. It models a date & time for you. You just didn't create the structure yourself. Just put the Date property directly on your MainViewModel and use it.

网友答案:

Models dont NEED viewmodels. But if the model is the basis for data being presented on the UI then the viewmodel will need to hold an instance of that model. It may even be better to not have a Date model at all and just hold the DateTime property in the viewmodel. Your question is based around too simple an example to really give you advice so maybe I can point you to a place where you can get some more broad information.

This code project project could help you learn more about how to design your current solution http://www.codeproject.com/Articles/100175/Model-View-ViewModel-MVVM-Explained.

It shows a good example of how the model and viewmodel interact and hopefully will help your understanding of the concept

网友答案:

If the date belongs to the main (parent) view, simply incorporate it in MainViewModel as a property, that you will later bind your view to. I don't see a reason to create a new class for Date, we already have one in .NET. If you need to update time (like digital clock), simply do it by asynchronously updating the member and propagating changes to the View by implementing OnPropertyChanged.

ViewModel is a mediator between the model and the view. It's something that transforms your data (model) the way it can be presented later by views. Does this make sense for you? You'll usually end up with one viewmodel for one view, but that doesn't mean you necessarily need to have the exact number of models. You can have 10 models and only a single view with a single viewmodel, and it wouldn't matter. MVVM is about separation, it doesn't mean you have 1/1/1 situation.

Furthermore, Model is kinda abstract term for multiple things. It doesn't only mean entity classes, but it's pretty much everything that isn't dependent on UI (data access, business logic, services..)

Opinions are different on this topic, but I understand MVVM like this:

You have some classes and some logic between them, that create your core structure. It usually contains data access and some services as well. Then you decide you need some UI, so you will use Views as a presenting media, but you need to fill the gap between your app and your UI. And here come viewmodels, that's their role.

Does this make sense for you?

网友答案:

The ViewModel is just the Model for the View. It is representation. The VIewModel uses data from your Model to bind it to the View. Every View should have its ViewModel but thats not the case for Models and ViewModels. In your case the MainView has just a MainViewModel. In MainViewModel you would write something like this :

private Date myDate;
public Date MyDate{get{return myDate;}}

And in MainViewModel you just bind to MyDate property. You can see that MainViewModel uses the Date Model class creates a property and the View Binds to it. And the answer is no you dont need a ViewModel for each Model class.

相关阅读:
Top