TOC

This article is currently in the process of being translated into Slovak (~23% done).

Commands:

Introduction to WPF Commands

V predchádzajúcej kapitole tohto tutoriálu sme si hovorili o tom, ako obslúžiť udalosti, napr. keď užívateľ klikne na tlačidlo alebo položku ponuky. V modernom užívateľskom rozhraní (UI) je typické, že funkcia je dostupná z viacerých miest, vyvolaná rôznymi akciami používateľa.

Napríklad ak máte typické rozhranie s hlavnou ponukou a sadou panelov s nástrojmi, akcia ako Nový alebo Otvoriť môže byť dostupná v ponuke, na paneli nástrojov, v kontextovej ponuke (napríklad po kliknutí pravým tlačidlom myši na hlavnú oblasť aplikácie) a klávesovou skratkou Ctrl + N a Ctrl + O.

Každá z týchto akcií musí vykonávať úplne rovnakú časť kódu, takže v aplikácii WinForms budete musieť definovať udalosť pre každú z nich a potom zavolať spoločnú funkciu. V prípade vyššie uvedeného príkladu by to viedlo k najmenej trom obslúžnym udalostiam a nejakému kódu pre obsluhu klávesovej skratky. To nie je ideálna situácia.

Commands

With WPF, Microsoft is trying to remedy that with a concept called commands. It allows you to define actions in one place and then refer to them from all your user interface controls like menu items, toolbar buttons and so on. WPF will also listen for keyboard shortcuts and pass them along to the proper command, if any, making it the ideal way to offer keyboard shortcuts in an application.

Commands also solve another hassle when dealing with multiple entrances to the same function. In a WinForms application, you would be responsible for writing code that could disable user interface elements when the action was not available. For instance, if your application was able to use a clipboard command like Cut, but only when text was selected, you would have to manually enable and disable the main menu item, the toolbar button and the context menu item each time text selection changed.

With WPF commands, this is centralized. With one method you decide whether or not a given command can be executed, and then WPF toggles all the subscribing interface elements on or off automatically. This makes it so much easier to create a responsive and dynamic application!

Command bindings

Commands don't actually do anything by them self. At the root, they consist of the ICommand interface, which only defines an event and two methods: Execute() and CanExecute(). The first one is for performing the actual action, while the second one is for determining whether the action is currently available. To perform the actual action of the command, you need a link between the command and your code and this is where the CommandBinding comes into play.

A CommandBinding is usually defined on a Window or a UserControl, and holds a references to the Command that it handles, as well as the actual event handlers for dealing with the Execute() and CanExecute() events of the Command.

Pre-defined commands

You can of course implement your own commands, which we'll look into in one of the next chapters, but to make it easier for you, the WPF team has defined over 100 commonly used commands that you can use. They have been divided into 5 categories, called ApplicationCommands, NavigationCommands, MediaCommands, EditingCommands and ComponentCommands. Especially ApplicationCommands contains commands for a lot of very frequently used actions like New, Open, Save and Cut, Copy and Paste.

Summary

Commands help you to respond to a common action from several different sources, using a single event handler. It also makes it a lot easier to enable and disable user interface elements based on the current availability and state. This was all theory, but in the next chapters we'll discuss how commands are used and how you define your own custom commands.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!