Command In MVVM

Introduction

This blogdescribe 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 a 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 for command in the .net framework. Previously .Net Framework was using event and event handler for notifying subscribers (instance that subscribes ) by the publisher(instance that exposes the event) when there is a need, but some problems were there with these approaches discussed below.

  1. 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 of memory leaks in .NET.
  2. Problem:- 2 Because of tight coupling between an event and it's a 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.

Command provides more control over the event in WPF, for example, you can put conditions, to check when your code will execute

Related Articles

  1. WPF Dispatcher
  2. ICommand In MVVM
  3. WPF Data Validation: IDataErrorInfo
  4. Data Validation in WPF
  5. Resource in WPF

Summary

In this blog, we have discussed Command and the reasonable use of the command, I hope this article may helpful to you.

Thanks


Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال