UraniumUI icon indicating copy to clipboard operation
UraniumUI copied to clipboard

[.NET9] Validations DataAnnotations not working When Release build

Open pkozak2 opened this issue 11 months ago • 2 comments

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:

Image

Released apk:

Image

Image

Test repository (included apk file): https://github.com/pkozak2/UraniumUIValidationsTest

pkozak2 avatar Jan 29 '25 15:01 pkozak2

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 💪

NiX3r avatar Jan 30 '25 07:01 NiX3r

in your ViewModel you have one property called email but you're binding it like Email with capital letter.

Yes, but when using CommunityToolkit.Mvvm package and attribute [ObservableProperty] toolkit will generate Email with capital letter:

Image

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: Image

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

pkozak2 avatar Jan 30 '25 08:01 pkozak2

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.

mgierlasinski avatar Nov 16 '25 19:11 mgierlasinski

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

enisn avatar Nov 17 '25 07:11 enisn

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.

mgierlasinski avatar Nov 17 '25 18:11 mgierlasinski