4.2 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T02:42:33.604683+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 63672ade54ed35d0 |
Commands
Purpose
This module implements a concrete, reusable ICommand-based command class (RelayCommand) for WPF applications, enabling decoupling of UI actions (e.g., button clicks) from their execution logic. It serves as a lightweight command adapter that forwards Execute and CanExecute calls to user-provided delegates, supporting both mandatory execution logic and optional, parameterized execution guards. This pattern is foundational for implementing MVVM-style command binding in WPF.
Public Interface
All members are public and part of the RelayCommand class.
-
RelayCommand(Action<object> execute)
Constructor. Initializes the command with an execution delegate and noCanExecuteguard (i.e.,CanExecutealways returnstrue). ThrowsArgumentNullExceptionifexecuteisnull. -
RelayCommand(Action<object> execute, Predicate<object> canExecute)
Constructor. Initializes the command with both execution and guard delegates. ThrowsArgumentNullExceptionifexecuteisnull. ThecanExecuteparameter may benull, in which caseCanExecutedefaults totrue. -
bool CanExecute(object parameter)
ImplementsICommand.CanExecute. Invokes the stored_canExecutepredicate (if non-null) withparameter; otherwise returnstrue. -
void Execute(object parameter)
ImplementsICommand.Execute. Invokes the stored_executeaction withparameter. No validation is performed onparameterbeyond null-safety of the delegate itself. -
event EventHandler CanExecuteChanged
ImplementsICommand.CanExecuteChanged. Subscribes/unsubscribes toCommandManager.RequerySuggested, enabling automatic re-evaluation ofCanExecutewhen WPF detects relevant state changes (e.g., keyboard/mouse input, focus changes).
Invariants
_executeis never null after construction (enforced viaArgumentNullExceptionin both constructors)._canExecutemay benull; if so,CanExecutealways returnstrue.CanExecuteChangedevent handlers are always attached toCommandManager.RequerySuggested, ensuring WPF’s command system triggers requery logic.- No explicit validation is performed on the
parameterpassed toExecuteorCanExecute; nulls are passed directly to the delegates.
Dependencies
- External Dependencies:
System(forAction<T>,Predicate<T>,ArgumentNullException)System.Windows.Input(forICommand,CommandManager)
- Internal Dependencies:
- No other modules in the codebase are referenced (self-contained).
- Depended Upon:
- Likely consumed by WPF UI layers (e.g.,
Button.Commandbindings) and view models throughout theDTS.Viewersubsystem.
- Likely consumed by WPF UI layers (e.g.,
Gotchas
- No manual
CanExecuteChangedraising: The class relies solely onCommandManager.RequerySuggestedto triggerCanExecutere-evaluation. If the command’s executability depends on non-UI state changes (e.g., background thread updates), callers must manually invokeCommandManager.InvalidateRequerySuggested()(or similar) to force updates. - Parameter handling: The
parameterpassed toExecute/CanExecuteis unvalidated. Passingnullis permitted and will be forwarded to the delegates—consumers must handlenullparameters explicitly if needed. - Thread affinity:
CommandManager.RequerySuggestedis raised on the UI thread. IfCanExecutedelegates access non-UI-thread resources, thread-safety must be ensured by the caller. - No disposal pattern: The class does not implement
IDisposable, and event subscriptions (viaCanExecuteChanged) are not explicitly cleaned up—relying on WPF’s lifetime management. This is acceptable for typical view model lifetimes but may cause leaks in long-lived command instances. - No async support:
Executeis synchronous; asynchronous operations require manual wrapping (e.g.,async voidor fire-and-forget), which is error-prone.