64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using System.Linq;
|
|
using Avalonia;
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
using Avalonia.Data.Core.Plugins;
|
|
using Avalonia.Markup.Xaml;
|
|
using GazpromApp.Utils;
|
|
using GazpromApp.ViewModels;
|
|
using GazpromApp.Views;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace GazpromApp;
|
|
|
|
public class App : Application
|
|
{
|
|
private static ServiceProvider? Services { get; set; }
|
|
|
|
public override void Initialize()
|
|
{
|
|
AvaloniaXamlLoader.Load(this);
|
|
}
|
|
|
|
public override void OnFrameworkInitializationCompleted()
|
|
{
|
|
var services = new ServiceCollection();
|
|
|
|
services.AddDbContext<AppDbContext>(options =>
|
|
{
|
|
var dbPath = new AppDbContext().DbPath;
|
|
options.UseSqlite($"Data Source={dbPath}");
|
|
});
|
|
|
|
services.AddTransient<MainWindowViewModel>();
|
|
|
|
Services = services.BuildServiceProvider();
|
|
|
|
var dbContext = Services.GetRequiredService<AppDbContext>();
|
|
dbContext.Database.EnsureCreated();
|
|
|
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
{
|
|
DisableAvaloniaDataAnnotationValidation();
|
|
desktop.MainWindow = new MainWindow
|
|
{
|
|
DataContext = Services.GetRequiredService<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);
|
|
}
|
|
}
|
|
} |