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

BindingPlugins.DataValidators.RemoveAt(0) is too magical in the template #262

Open
duongphuhiep opened this issue Aug 2, 2024 · 2 comments

Comments

@duongphuhiep
Copy link
Contributor

duongphuhiep commented Aug 2, 2024

Is your feature request related to a problem? Please describe.

The value 0 is too "magic" IMO, if we upgrade the Avalonia version then the value 0 might not apply and might cause unwanted regression.

BindingPlugins.DataValidators.RemoveAt(0);

// Line below is needed to remove Avalonia data validation.
// Without this line you will get duplicate validations from both Avalonia and CT
BindingPlugins.DataValidators.RemoveAt(0);

Describe the solution you'd like

Replace the 0 with something less magic which stand more chance for future Avalonia update.

// Line below is needed to remove Avalonia data validation.
// Without this line you will get duplicate validations from both Avalonia and CT
BindingPlugins.DataValidators.Remove(BindingPlugins.DataValidators.First(plugin => plugin is DataAnnotationsValidationPlugin));

Describe alternatives you've considered

Find a way in avalonia or CT to disable this validation by configuration, instead of adding it then removing it.

Additional context

No response

@duongphuhiep
Copy link
Contributor Author

duongphuhiep commented Aug 2, 2024

OR the template should simply follow the official documentation

private void DisableAvaloniaDataAnnotationValidation()
{
    // Get an array of plugins to remove
    var dataValidationPluginsToRemove =
        BindingPlugins.DataValidators.OfType<DataAnnotationsValidationPlugin>().ToArray();

    // remove each entry found
    foreach (var plugin in dataValidationPluginsToRemove)
    {
        BindingPlugins.DataValidators.Remove(plugin);
    }
}

public override void OnFrameworkInitializationCompleted()
{
    // Avoid duplicate validations from both Avalonia and CT. More info: https://docs.avaloniaui.net/docs/guides/development-guides/data-validation#manage-validationplugins
    DisableAvaloniaDataAnnotationValidation();

    if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
    {
        desktop.MainWindow = new MainWindow
        {
            DataContext = new MainWindowViewModel(),
        };
    }

    base.OnFrameworkInitializationCompleted();
}

@maxkatz6
Copy link
Member

maxkatz6 commented Aug 6, 2024

I think DataAnnotationsValidationPlugin was made internal at some point temporary, and then was reverted. Which is why templates are changed, but documentation wasn't.

PRs are welcomed to change it back in the templates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants