Close Command WPF
I have just started learning WPF. I currently have the MenuItem File -> EXIT on my main window.
I want to know how i should add functionality to the Exit MenuItem. As i see it there are 3 main choices
1) Add an Event handler to the Click event of the Exit menuitem. Then code the event handler in the window code behind
2) Add a Close command to to Exit menuitem and add a binding in the main window that handles the command. Again this involves adding code to the window code behind.
3) Add a command to the Exit menuItem that calls a method in the ViewModel. This removes code from the views code behind, but means the ViewModel needs to hold a reference to the View.
Which approach would best suit this situation?
I don't want to get to hung up on not putting any code at all into the Views code-behind. However, 开发者_运维知识库since this is action only reaaly involves the view, maybe method 1 or 2 would be best. Any thoughts?
If it is a basic Exit command, I see no problem in putting it in the code behind.
If you start wanting to execute other code during Exit than I would either put your Exit code in a ViewModel somewhere and bind to it, or use something like MVVM Light's Messenger
class or Prism's EventAggregator
to raise a CloseApplicationEvent
so other objects can handle themselves during shutdown.
Well I came across the exact same scenario and this is what I did:
Since I'm using MVVMLight Toolkit hence all the ViewModels are created in a centralised location i.e. the ViewModelLocator. This class also contain static properties through which you can access these viewmodels.
Hence I declared an event in the viewmodel as follows:
public event EventHandler RequestClose;
Next I provided an implementation to it in the code-behind file from which I am opening the viewmodel's associated view as follows:
var view = new NewView(); view.Owner = this; ViewModelLocator.NewViewViewModelStatic.RequestClose += (s, e) => { view.Close(); };
Now whenever I needed to close the view from the viewmodel, I would just call:
RequestClose(this, new EventArgs());
Hope it helps. Kindly let me know, if you have any confusion.
How? Unfortunately, there is no clear cut choice with this.
As for the first two options, handling the closing of the view can be said to be the purview of the view, so using codebehind is acceptable. You can do this with a Button by setting the IsCancel property to true
. This closes the Window, and if you do not have to handle the closing from within the ViewModel, is perfectly fine.
As for the third option, I have done this by binding the CommandArgument to the Window itself. The ICommand gets the Window as its argument in the Execute method. From this ICommand you can handle whatever you need within the ViewModel and close the Window.
Is this good or bad? Well, some purists might argue that this puts a dependency on the ViewModel that breaks compatibility with other view types. Practically, I think its fine. You can do calisthenics to keep your ViewModel absolutely pure, but at what cost?
精彩评论