Paul Krill
Editor at Large

Microsoft .NET Community Toolkit 8.2 shines on MVVM

news
Apr 28, 20232 mins
C#Development Libraries and FrameworksMicrosoft .NET

Custom attributes for [RelayCommand], new property change hooks for [ObservableProperty] fields, MVVM code fixers, and MVVM source generator optimizations are also featured in the update.

Microsoft has launched .NET Community Toolkit 8.2, again with improvements for MVVM (model-view-viewmodel) development. The preceding 8.1 release, published in January, also focused on MVVM capabilities.

The .NET Community Toolkit features a collection of APIs and helpers for .NET developers, agnostic of any UI platform. Source code for version 8.2, announced April 27, can be accessed from GitHub.

With the new release of .NET Community Toolkit, the included MVVM Toolkit now supports custom attributes when using [RelayCommand]. Developers can leverage the native field: and property: C# syntax to indicate targets of custom attributes, giving them full control over attributes for generated members when using [RelayCommand] to generate an MVVM command. Microsoft said support for custom attributes was particularly useful when using a viewmodel that needs to support JSON serialization and must explicitly ignore the generated property.

Also in the MVVM Toolkit in 8.2, two new property change hooks now are generated for all [ObservableProperty] fields. This fixes a situation in which using [ObservableProperty] lacked the infrastructure to easily inject logic for state changes on old and new values being set. A common scenario in MVVM is to have a “selected item” observable property, representing, for example, the currently selected user or nested viewmodel. In previous versions, when the value of the property changed, it was not uncommon to also have to make some adjustments to old and new instances.

The MVVM Toolkit in 8.2 also adds built-in code fixers for two previously added diagnostic analyzers. These analyzers produce a warning when incorrectly accessing a field marked with [ObservableProperty] and when declaring a type with [ObservableProperty] and similar attributes when using inheritance.

The update also brings performance improvements to MVVM source generators, focused on optimizing incremental pipelines to minimize memory usage and ensure that no unnecessary objects will be kept alive across concurrent executions. In another improvement, all generated types and members now are decorated with full XML docs, making inspected code produced by the source generators easier to understand.

With the .NET Community Toolkit 8.2 release, runtime performance has been improved as well. The .NET Community Toolkit includes these libraries:

  • CommunityToolkit.Common
  • CommunityToolkit.Mvvm (aka “Microsoft MVVM Toolkit”)
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance

These components are used in several inbox apps that ship with Windows, such as the Microsoft Store and the Photos app.