This article is currently in the process of being translated into Vietnamese (~29% done).
If you are fluent in Vietnamese, then please help us - just point to any untranslated element (highlighted with a yellow left border - remember that images should have their titles translated as well!) inside the article and click the translation button to get started. Or have a look at the current translation status for the Vietnamese language.
If you see a translation that you think looks wrong, then please consult the original article to make sure and then use the vote button to let us know about it.
Please help us by translating the following metadata for the article/chapter, if they are not already translated.
If you are not satisfied with the translation of a specific metadata item, you may vote it down - when it reaches a certain negative threshold, it will be removed. Please only submit an altered translation of a metadata item if you have good reasons to do so!
Introduction to WPF Commands
Ở các chương trước của bài hướng dẫn này, chúng ta đã nói về cách bắt sự kiện, ví dụ như người dùng bấm bào một nút hoặc danh sách các mục (menu item). Trong giao diện người dùng, nó là kiểu chức năng cho phép truy cập ở một vài nơi, được gọi bởi các hành động khác nhau của người dùng.
Lấy ví dụ, nếu bạn có giao diện người dùng điển hình với danh sách chức năng và thanh công cụ, các hành động như kiểu: Tạo mới hoặc mở có sẵn trong danh sách trong thanh công cụ, trong danh mục (Ví dụ như nhấn trái chuột vào khu vực ứng dụng chính) và phím tắt từ bàn phím như Ctrl+N và Ctrl+O.
Tương ứng với từng hành động sẽ tương ứng với chính xác từng đoạn mã, trong các ứng dụng WinForm, bạn sẽ phải định nghĩa các sự kiện cho chúng và sau đó gọi các hàm (function). Với ví trụ trước, có ít nhất 3 sự kiện và vài đoạn mã để xử lý phím tắt. Nó không phải ý tưởng tốt.
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!
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.
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.
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.