Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BindableProperty validation does not throw an ArgumentException, although it is documented as such #25823

Open
MSwit opened this issue Nov 13, 2024 · 1 comment
Labels
migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert platform/android 🤖 platform/iOS 🍎 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@MSwit
Copy link

MSwit commented Nov 13, 2024

Description

When a validation callback of a BindableProperty returns false,
no Exeption is raised as documented here

Example:

public class CustomControl : ContentView
    {
        public static readonly BindableProperty CustomTextProperty = BindableProperty.Create(
            nameof(CustomText),
            typeof(string),
            typeof(CustomControl),
            defaultValue: string.Empty,
            validateValue: (bindable, value) =>
            {
                // This validator returns false for empty strings
                // We expect this to throw an ArgumentException, but it doesn't
                return !string.IsNullOrEmpty((string)value);
            }
        );

        public string CustomText
        {
            get => (string)GetValue(CustomTextProperty);
            set => SetValue(CustomTextProperty, value);
        }

        public CustomControl()
        {
            // Basic layout for demonstration
            Content = new Label
            {
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center,
            };
            CustomText = "some valid text";

            // Set up the binding correctly
            ((Label)Content).SetBinding(Label.TextProperty, new Binding(nameof(CustomText), source: this));
        }
    }

Steps to Reproduce

Link to the reproduction link below

Link to public reproduction project repository

https://github.com/zauberzeug/Maui_BindableProperty_validation

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Not sure, did not test other versions, Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, I was not able test on other platforms

Affected platform versions

iOS 17, Android 13

Did you find any workaround?

Throw an Exeption inside the validation callback.

   public static readonly BindableProperty CustomTextProperty = BindableProperty.Create(
            nameof(CustomText),
            typeof(string),
            typeof(CustomControl),
            defaultValue: string.Empty,
            validateValue: (bindable, value) =>
            {
                if (string.IsNullOrEmpty((string)value))
                    throw new ArgumentException();
                return true;
            }
        );
@MSwit MSwit added the t/bug Something isn't working label Nov 13, 2024
@ninachen03 ninachen03 added platform/android 🤖 platform/iOS 🍎 s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert labels Nov 14, 2024
@ninachen03
Copy link

This issue has been verified using Visual Studio 17.13.0 Preview 1.0 (8.0.93 & 8.0.3). Can repro on iOS/Android platform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert platform/android 🤖 platform/iOS 🍎 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants