Update adds custom attributes for [RelayCommand], property change hooks for [ObservableProperty], and optimizations to MVVM tools
Microsoft has introduced .NET Community Toolkit 8.2, focusing on enhancing MVVM (model-view-viewmodel) development, continuing the trend from its earlier 8.1 release. The toolkit provides a range of APIs and helpers for .NET developers, and it remains platform-agnostic in terms of UI frameworks. Version 8.2 was announced on April 27, and its source code is available on GitHub.
A major highlight of this release is the addition of custom attributes for [RelayCommand]. With this new capability, developers can use C#’s field:
and property:
syntax to specify the target of custom attributes for generated members. This is especially beneficial in scenarios where viewmodels need to support features like JSON serialization but need to exclude the auto-generated properties. Custom attributes provide fine-grained control, making it easier to integrate MVVM with other frameworks that require such functionality.
Another key improvement comes in the form of property change hooks for [ObservableProperty] fields. In prior versions, developers often faced challenges when trying to implement custom logic for state changes when observable properties updated their values. This release fixes that limitation by automatically generating property change hooks, enabling developers to handle transitions between old and new values more smoothly. This enhancement is particularly useful in scenarios like managing a “selected item” in a viewmodel, where additional logic is required when the selected item changes.
.NET Community Toolkit 8.2 also includes code fixers for diagnostic analyzers that were previously introduced. These analyzers warn developers about incorrect field access or improper use of [ObservableProperty] attributes in inherited classes. The built-in code fixers streamline the developer experience, helping to catch and correct these mistakes early.
In terms of performance, the MVVM source generators in this release have undergone optimizations aimed at minimizing memory usage. Incremental pipeline optimizations ensure that unnecessary objects are not retained during concurrent executions. This leads to improved performance when generating MVVM-related code, especially in large applications where memory efficiency is critical.
Additionally, all generated types and members in this version now come with complete XML documentation. This makes the auto-generated code easier to understand, benefiting developers who need to inspect or modify the source-generated code. These documentation enhancements improve the overall clarity of the codebase, particularly when dealing with complex MVVM structures.