问题
I've craeted a sample with mainView and 2 other views (usercontrols). I've placed a button "close" on the child view and i want to close that view. there is a command attached to that button, and when close is pressed i ask the ViewModelLocator to clean it.
BUt- the view still being displayed.. What i'm doing wrong? How can i close a userControl view with mvvm-light?
private RelayCommand _closeCommand;
public RelayCommand CloseCommand
{
get
{
if (_closeCommand == null)
{
_closeCommand = new RelayCommand(()=>
ViewModelLocator.ClearAllChannels(),
);
}
return _closeCommand;
}
}
ViewModelLocator function:
public static void ClearAllChannels()
{
if (_allChannels != null)
{
_allChannels.Cleanup();
_allChannels = null;
}
}
回答1:
The ViewModelLocator does not actually host your views. It just provides a way to look up the ViewModel that supports a particular view.
So you need to ask the control that hosts your views (probably a Window or Frame) to close them.
来源:https://stackoverflow.com/questions/3042741/how-to-close-a-usercontrol-view-in-mvvm-light