[.NET9] Validations DataAnnotations not working When Release build
I creating .net 9 MAUI app. In debug mode - validations based on DataAnnotations working as expected. But, when I publish my app in release mode - validations not working. There is no Errors labels and warning icon in material textfield.
I copied DataAnnotationsBehavior.cs to my project, changed xaml to use my behavior, and I added some console log lines to file. Code returns in this line: https://github.com/enisn/UraniumUI/blob/develop/src/UraniumUI.Validations.DataAnnotations/Validations/DataAnnotationsBehavior.cs#L44
Debug mode:
Released apk:
Test repository (included apk file): https://github.com/pkozak2/UraniumUIValidationsTest
Hey there 👋
I take a quick look at your issue. I don't know if it will be issue in MAUI. But in your ViewModel you have one property called email but you're binding it like Email with capital letter.
Btw in C# your variables in ViewModels should start with capital letters. Not in case of using private variable with getters and setters of course. You can name it as you wish, but this is standard 💪
in your ViewModel you have one property called
Yes, but when using CommunityToolkit.Mvvm package and attribute [ObservableProperty] toolkit will generate Email with capital letter:
Edit: I added next field with default private, and public with SetProperty in set. Still validations not working in release mode.
Edit2:
I added some logs:
DataAnnotationsBehavior, 1dc7d095-7371-4e16-bb11-f1ceb613863f Apply, UraniumUI.Material.Controls.TextField, Microsoft.Maui.Controls.Internals.TypedBinding`2[UraniumUITestValidations.MainPageViewModel,System.String]
TypedBinding is not inherited from from Binding in maui code: https://github.com/dotnet/maui/blob/34f98acea96a6d16dbac2638bee62cbc9939b276/src/Controls/src/Core/TypedBinding.cs#L74
Same issue here, I had to quickly switch to InputKit Validations which was not ideal for me, I already have unit tests for view model validation scenarios.
It might be related to Assembly Trimming since it uses reflection, it's not possible to detect that methods are used at build time. Let me investigate for more details. You can try configuring assembly trimming in a short term
I thought adding UraniumUI.Validations.DataAnnotations and maybe System.ComponentModel.DataAnnotations will solve it.
After trying different combinations I ended up with this, it didn’t help:
<TrimmerRootAssembly Include="UraniumUI.Validations.DataAnnotations" RootMode="All" />
<TrimmerRootAssembly Include="System.ComponentModel.DataAnnotations" RootMode="All" />
<TrimmerRootAssembly Include="CommunityToolkit.Mvvm" RootMode="All" />
<TrimmerRootAssembly Include="CommunityToolkit.Maui.Core" RootMode="All" />
<TrimmerRootAssembly Include="System.ComponentModel" RootMode="All" />
I'm using ObservableValidator from CommunityToolkit.Mvvm as base class so I've added just in case.