-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.axaml.cs
More file actions
65 lines (53 loc) · 2.21 KB
/
App.axaml.cs
File metadata and controls
65 lines (53 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.Linq;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using PostCodeSerialMonitor.ViewModels;
using PostCodeSerialMonitor.Views;
using Microsoft.Extensions.DependencyInjection;
namespace PostCodeSerialMonitor;
public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
// If you use CommunityToolkit, 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);
// Register all the services needed for the application to run
var collection = new ServiceCollection();
collection.AddCommonServices();
// Creates a ServiceProvider containing services from the provided IServiceCollection
var services = collection.BuildServiceProvider();
var mainWindowViewModel = services.GetRequiredService<MainWindowViewModel>();
//@todo Load language setting in cofig.json.
//
//Assets.Resources.Culture = new CultureInfo("en-US");
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
// Avoid duplicate validations from both Avalonia and the CommunityToolkit.
// More info: https://docs.avaloniaui.net/docs/guides/development-guides/data-validation#manage-validationplugins
DisableAvaloniaDataAnnotationValidation();
desktop.MainWindow = new MainWindow
{
DataContext = mainWindowViewModel,
};
}
base.OnFrameworkInitializationCompleted();
}
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);
}
}
}