简体   繁体   中英

MVVM Light - ViewModelLocator, set Propertys

Hello I need help with mvvm light.

I want to set properties in my constructor. But its calling only once. I have a viewModel and a view. I call the viewModel from another viewModel through ViewModelLocator. On the first time, when i call the viewModel all works fine, but when i call the viewModel in other times it doesn't work, because it was created the viewModel and its constructor already called. How can i reset(or whatever) the viewModelLocator so he call the constructor every time when i call the viewModelLocator.

In the code below, i am calling another viewModel:

var viewModel= (new ViewModelLocator()).TestVM;
viewModel.Item = _item;
Messenger.Default.Send(new NotificationMessage("testView"));

MVVMLight's ViewModelLocator isn't designed to be instanciated directly in your code. Inside its contructor you should register all dependencies you know. Your ViewModels would then take all dependencies as constructor parameters like this:

public MainViewModel(IMessenger messenger, IDialogService dialogService)
{
    // Use messenger and dialogService
    // ...
}

When you set a ViewModel as a DataContext in a View using your locator instance, the ViewModelLocator tries to resolve all neccessary dependencies.

Please note that the default behavior of the SimpleIoc container is to cache all created objects, so none of your constructors will be called twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM