Skip to content

Commit

Permalink
Merge pull request #263 from duongphuhiep/main
Browse files Browse the repository at this point in the history
issues/262: enhance dataValidator setup
  • Loading branch information
maxkatz6 authored Aug 13, 2024
2 parents 7d00e7b + 1bd0cae commit 43c5d48
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
5 changes: 2 additions & 3 deletions templates/csharp/app-mvvm/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"description": "The target version of Avalonia NuGet packages.",
"datatype": "text",
"replaces": "AvaloniaVersionTemplateParameter",
"defaultValue": "11.1.0"
"defaultValue": "11.1.2"
},
"UseCompiledBindings": {
"type": "parameter",
Expand Down Expand Up @@ -106,8 +106,7 @@
"exclude": [
"App.RemoveViewLocator.axaml"
]
},

}
]
}
],
Expand Down
22 changes: 19 additions & 3 deletions templates/csharp/app-mvvm/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if (CommunityToolkitChosen)
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using System.Linq;
#endif
using Avalonia.Markup.Xaml;
using AvaloniaAppTemplate.ViewModels;
Expand All @@ -22,9 +23,9 @@ public override void OnFrameworkInitializationCompleted()
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
#if (CommunityToolkitChosen)
// 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);
// Avoid duplicate validations from both Avalonia and the CommunityToolkit.
// More info: https://docs.avaloniaui.net/docs/guides/development-guides/data-validation#manage-validationplugins
DisableAvaloniaDataAnnotationValidation();
#endif
desktop.MainWindow = new MainWindow
{
Expand All @@ -34,4 +35,19 @@ public override void OnFrameworkInitializationCompleted()

base.OnFrameworkInitializationCompleted();
}

#if (CommunityToolkitChosen)
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);
}
}
#endif
}
22 changes: 19 additions & 3 deletions templates/csharp/xplat/AvaloniaTest/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if (CommunityToolkitChosen)
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using System.Linq;
#endif
using Avalonia.Markup.Xaml;
using AvaloniaTest.ViewModels;
Expand All @@ -22,9 +23,9 @@ public override void OnFrameworkInitializationCompleted()
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
#if (CommunityToolkitChosen)
// 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);
// Avoid duplicate validations from both Avalonia and the CommunityToolkit.
// More info: https://docs.avaloniaui.net/docs/guides/development-guides/data-validation#manage-validationplugins
DisableAvaloniaDataAnnotationValidation();
#endif
desktop.MainWindow = new MainWindow
{
Expand All @@ -41,4 +42,19 @@ public override void OnFrameworkInitializationCompleted()

base.OnFrameworkInitializationCompleted();
}

#if (CommunityToolkitChosen)
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);
}
}
#endif
}

0 comments on commit 43c5d48

Please sign in to comment.