4.1 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T03:19:06.518329+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 33db5b96b73a6f00 |
Commands
1. 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 supports optional conditional execution via a CanExecute predicate and integrates with WPF’s CommandManager to automatically re-evaluate command availability when UI state changes (e.g., focus or selection changes). Its role is to serve as a lightweight, standard command implementation for MVVM pattern adoption, avoiding boilerplate command classes per action.
2. Public Interface
-
RelayCommand(Action<object> execute)
Constructor. Initializes the command with an execution delegate and noCanExecutepredicate (i.e., always executable).- Throws
ArgumentNullExceptionifexecuteisnull.
- Throws
-
RelayCommand(Action<object> execute, Predicate<object> canExecute)
Constructor. Initializes the command with both execution and availability predicates.- Throws
ArgumentNullExceptionifexecuteisnull. canExecutemay benull, in which case the command is always executable.
- Throws
-
bool CanExecute(object parameter)
ImplementsICommand.CanExecute. Returnstrueif_canExecuteisnullor if_canExecute(parameter)returnstrue; otherwisefalse. -
event EventHandler CanExecuteChanged
ImplementsICommand.CanExecuteChanged. Subscribes/unsubscribes toCommandManager.RequerySuggested, triggering WPF to re-queryCanExecutewhen system events (e.g., keyboard/mouse input, focus changes) occur. -
void Execute(object parameter)
ImplementsICommand.Execute. Invokes_execute(parameter). No validation or exception handling is performed; exceptions propagate to the caller.
3. Invariants
_executeis nevernullafter construction (enforced viaArgumentNullExceptionin both constructors)._canExecutemay benull; in this case,CanExecutealways returnstrue.CanExecuteChangedevents are always routed toCommandManager.RequerySuggested; no custom event storage is used.ExecuteandCanExecuteare called with the sameparametervalue provided by the command source (e.g.,Button.CommandParameter).
4. Dependencies
- Dependencies on external modules:
System(forAction<T>,Predicate<T>,ArgumentNullException)System.Windows.Input(forICommand,CommandManager)
- Dependencies on other modules in the codebase: None (no internal
usingstatements beyond standard libraries). - Depended upon by: Presumably UI components (e.g.,
Button,MenuItem) in WPF views that bind commands via MVVM. Not visible in this file, but inferred fromICommandusage.
5. Gotchas
- No manual
CanExecuteChangedraising: SinceCanExecuteChangedis wired toCommandManager.RequerySuggested, the command’s executability is only re-evaluated onCommandManager-triggered events (e.g., user input). If the command’s availability depends on non-UI state (e.g., background task completion),CommandManager.InvalidateRequerySuggested()must be called externally to force re-evaluation. - No parameter validation:
Executepasses theparameterdirectly to_executewithout null-checking or type validation. If_executeexpects a specific type (e.g.,string), passing an incompatible type will cause a runtime exception. - Thread affinity:
Executeruns on the calling thread (typically the UI thread), but no thread-safety guarantees are provided. If invoked from a background thread, UI updates inside_executewill fail. - Memory leak risk: If subscribers to
CanExecuteChangedare not properly disposed, theCommandManagermay hold references indefinitely (though this is a general WPF pattern risk, not unique to this class).
None identified beyond the above.