#MAGTOOL COMMANDS WINDOWS#
Note that in Windows Presentation Foundation (WPF), the CanExecuteChanged event does not need to be raised manually. In XAML, when an instance of ICommand is bound to a control’s Command property through a data-binding, raising the CanExecuteChanged event will automatically call the CanExecute method, and the control will be enabled or disabled accordingly. The CanExecuteChanged event handler must be raised by the command implementation when the CanExecute method needs to be reevaluated.When used in XAML controls that support the Command property, the control will be automatically disabled if CanExecute returns false. The parameter is the same one as for the Execute method. If the return value is true, it means that the command can be executed. The method CanExecute(object) returns a Boolean.It has one parameter, which can be used to pass additional information from the caller to the command. The method Execute(object) is called when the command is actuated.The ICommand interface specifies three members: This interface is used a lot in MVVM applications, but it is useful not only in XAML-based apps.
What’s a Command?Ĭommands are an implementation of the ICommand interface that is part of the. And because they are loosely coupled, they won’t risk causing memory leaks. Because data-bindings are evaluated only at run time, they won’t cause a compilation error. Thankfully, there is a solution to this issue: using a command to expose the “event handler” and bind the UI element to that command by using a XAML data-binding. ((Frame)).Navigate(typeof (DetailsPage), item)
#MAGTOOL COMMANDS CODE#
This code would cause a compilation error.įigure 1. When an element of the template must be actuated, an event handler can be defined, but as a consequence, the DataTemplate cannot be moved into an external ResourceDictionary, as shown in Figure 1. This is especially an issue when working with list controls and associated DataTemplates. If it is not there (or if there is no attached code-behind file), the compilation will fail with an error. NET.Īnother consequence of the tight coupling between an event and its handler is that the event handler for a UI element declared in XAML must be found in the attached code-behind file. This is a frequent cause for memory leaks in. 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. 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. Most notably, this approach is used by UI elements to handle user events-for example, the click of a button, the movement of the mouse and many more.įor all their utility, event handlers have one problematic side effect: they can create a tight coupling between the instance that exposes the event and the instance that subscribes to it. It is used by a sensor (such as location, orientation, proximity and so on) to notify the class that uses it that a condition has changed (for example, the user has moved, the screen has rotated, the device is close to another one, and the like). NET, it is often used to call back a subscriber after a Web operation completes (or fails). This approach is fairly simple and successful for many scenarios. When the event is raised, the event-handling method is executed and the EventArgs instance carries additional information (if available) about what caused the event in the first place. On the other hand, the subscribers provide an EventHandler, which is typically a method with two parameters: the sender of the event and an instance of a class deriving from EventArgs. NET Framework has always been an event-based framework: a class exposes an event that is raised by the class instances when subscribers need to be notified.
In this article, I take a closer look at one of the important components of any Model-View-ViewModel application: the command. I also showed how adding design-time data makes it easier to work in Expression Blend or the Visual Studio designer in a visual manner. In previous installments of this series, I described the importance of decoupling the components of an application to make the application easier to unit test, maintain and extend. Volume 28 Number 05 MVVM - Commands, Rela圜ommands and EventToCommand