How renaming breaks MVVM Toolkit bindings
A CommunityToolkit.Mvvm view-model is mostly code you never wrote. You declare a field and an attribute; a source generator produces the public property, the PropertyChanged plumbing and the command object at compile time. That is the appeal — and it is also why obfuscating such an app trips people up. The names XAML binds to are not in your source. They are generated, they are public, and some…
The MVVM Toolkit binding process relies on the names XAML binds to, which are generated and resolved by string at runtime. When renaming an assembly, the challenge is determining which generated names the runtime looks up by string and which were already wired as direct references. To avoid binding issues, it is crucial to keep the distinction in mind and apply the correct renaming strategy.
The generated IL code shows two types of names: strings and tokens. String names, such as FirstName and DisplayName, are used in property getters and setters, while token names, like FirstName in OnPropertyChanging(FirstName), are used for change notifications. Binding the runtime attempts to match the string name, and renaming the property will cause binding errors.
Commands, such as SaveCommand, are handled similarly, with the command property being resolved by string and other command-related names being kept. The recommended approach to renaming is to keep the public properties and commands on every view-model while renaming everything else in the assembly. This way, the binding layer continues to function correctly, and the rest of the code remains obfuscated and secure.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.