I ran into the issue of properly closing a Window (View) from my ViewModel a while ago. How does one do this without breaking MVVM or adding too much complexity? Since I’m developing my current project using Prism and my IUnityContainers are injected into my ViewModels, why not just inject this.Close() also?
/**** Window (View) ****/
...
public Window(IUnityContainer unityContainer)
{
InitializeComponent();
this.DataContext = new ViewModel(unityContainer, this.Close);
}
/**** End Window (View) ****/
/**** ViewModel ****/
public class ViewModel
{
public ViewModel(IUnityContainer unityContainwer, Action closeAction)
{
...
CloseCommand = new DelegateCommand(closeAction);
}
public DelegateCommand CloseCommand
{
get;
private set;
}
}
/**** End ViewModel ****/
Does this break MVVM at all? Is testability decreased because I injected the this.Close() method into my ViewModel? I would argue not since I’ve used the Action delegate. This seems to be the easiest/simplest way to do what I (and many others) want. Now there may be some purists out there that don’t want any user entered code in the code-behind, but as you might have guessed, I’m no purist.
No comments:
Post a Comment