Introduction
This article describe about Command in MVVM and the reason, why one should use command rather than event in WPF MVVM Pattern.
Getting Started
Command in Windows Presentation Framework(WPF) with Model View ViewModel(MVVM) pattern provides a mechanism for binding action with view.In programmatically we can say command is nothing but implementation of ICommand interface.
Need of Command
It's there to think that if event handling is there, so why the need of command in .net framework. Previously .Net Framework was using event and event handler for notifying subscriber(instance that subscribes ) by publisher(instance that exposes the event) when there is need, but some problems were there with these approch discussed below.- Problem :- 1 Event handlers can create a tight coupling between the instance that exposes the event and the instance that subscribes to it. Because the system needs to keep track of event handlers so that they can be executed when the event is raised, but the strong link this creates might prevent garbage collection. Of course, this isn’t an issue if the event handler is a static method, but it is not always possible to handle all events with static methods only. This is a frequent cause for memory leaks in .NET.
- Problem :- 2 Because of tight coupling between an event and it's handler, the event handler for a UI element declared in XAML must be found in the attached code-behind file. If it is not there, the compilation will fail with an error.
Related Articles
Summary
In this artcie we have discussed about Command and the reason use of comman, hope this article may helpful to you.
Thanks
Kailash Chandra Behera
Comments
Post a Comment