madaxe2020
Well-known member
- Joined
- Sep 7, 2020
- Messages
- 50
- Programming Experience
- 5-10
Doe anybody Know why my close event handler is no longer working after adding dependency injection
thanks
Madaxe
thanks
Madaxe
StartUp:
namespace CATIAAppsSideWidget
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private MainWindow _mainwindow;
private ILogger _logger;
private void OnStartup(object sender, StartupEventArgs e)
{
IConfigurationBuilder builder = new ConfigurationBuilder();
BuildConfig(builder);
_logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Build())
.Enrich.FromLogContext()
.CreateLogger();
_logger.Information("Application Starting");
_logger.Information("Adding Dependancies");
var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddSingleton(_logger);
services.AddTransient<ContextWindowMenu>();
services.AddTransient<Window, MainWindow>();
services.AddTransient<CATIAV6Connection>();
services.AddTransient<ISQLiteHelper,SQLiteHelper>();
services.AddTransient<ICATIAAppsSideWidgetContextFactory,CATIAAppsSideWidgetContextFactory>();
services.AddTransient<ICATIAAppProvider,CATIAAppProvider>();
services.AddTransient<ICATIAAppCollectionModel,CATIAAppCollectionModel>();
services.AddTransient<ICATIAAppsSideWidgetModel,CATIAAppsSideWidgetModel>();
services.AddTransient<ICATIAAppsSideWidgetViewModel,CATIAAppsSideWidgetViewModel>();
})
.UseSerilog()
.Build();
if (ActivatorUtilities.CreateInstance<CATIAV6Connection>(host.Services).Connected == true)
{
_logger.Information("Creating the Main UI.");
_mainwindow = ActivatorUtilities.CreateInstance<MainWindow>(host.Services);
_logger.Information("Setting the UI DataContext.");
_mainwindow.DataContext = ActivatorUtilities.CreateInstance<CATIAAppsSideWidgetViewModel>(host.Services);
_logger.Information("Adding Close Event Handler.");
_mainwindow.Closed += new EventHandler(MainWidget_Closed);
_logger.Information("Showing UI.");
_mainwindow.Show();
}
else
{
MessageBox.Show("Failled to Connect to an Active CATIA Session, Please Ensure CATIA is Running.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public void MainWidget_Closed(object sender, EventArgs e)
{
_logger.Information("Main UI Closing.");
_mainwindow.Closed -= new EventHandler(MainWidget_Closed);
_mainwindow = null;
}
private static void BuildConfig(IConfigurationBuilder builder)
{
builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables();
}
}
}