admin管理员组

文章数量:1402978

I am working on a WPF project using .NET 8 and C# 12. I am trying to implement Dependency Injection using Microsoft.Extensions.DependencyInjection. However, I am encountering an issue where the MainWindow does not open when I try to resolve FattureFornitoriViewModel in the constructor of MainViewModel. Here is the relevant code:

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows;
using WpfAppFatture.Repository;
using WpfAppFatture.ViewModel;

namespace WpfAppFatture
{
  public partial class App : Application
  {
    public IServiceProvider? ServiceProvider { get; private set; }
    public static App Me => (App)Application.Current;

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        try
        {
            var serviceCollection = new ServiceCollection();
            ConfigureServices(serviceCollection);
            ServiceProvider = serviceCollection.BuildServiceProvider();

            var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
            mainWindow.Show();
        }
        catch (Exception ex)
        {
            MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }

    private void ConfigureServices(ServiceCollection services)
    {
        services.AddSingleton<MainViewModel>();
        services.AddSingleton<MainWindow>();
        services.AddSingleton<RepoFattureFornitori>();
        services.AddTransient<FattureFornitoriViewModel>();
    }

    protected override void OnExit(ExitEventArgs e)
    {
        base.OnExit(e);
        if (ServiceProvider is IDisposable)
        {
            ((IDisposable)ServiceProvider).Dispose();
        }
    }
  }
}

MainWindow.xaml.cs:

using System.Windows;

namespace WpfAppFatture
{
  public partial class MainWindow : Window
  {
    public MainWindow(MainViewModel vm)
    {
        InitializeComponent();
        DataContext = vm;
    }
  }
}

MainViewModel.cs:

using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.ObjectModel;

namespace WpfAppFatture.ViewModel
{
  public partial class MainViewModel : ObservableObject, IMainViewModel
  {
    private readonly IServiceProvider _serviceProvider;

    public MainViewModel(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
        try
        {
            FattureFornitori = _serviceProvider.GetRequiredService<FattureFornitoriViewModel>();
        }
        catch (Exception ex)
        {
            // Log the exception or handle it as needed
            Console.WriteLine($"An error occurred while resolving FattureFornitoriViewModel: {ex.Message}");
        }
    }

    [ObservableProperty] ObservableCollection<BaseTabViewModel> _tabs = new();
    [ObservableProperty] BaseTabViewModel? _selectedTab;
    [ObservableProperty] FattureFornitoriViewModel? _fattureFornitori;

    public void AddTab(BaseTabViewModel tab)
    {
        Tabs.Add(tab);
    }

    public void RemoveTab(BaseTabViewModel tab)
    {
        Tabs.Remove(tab);
        if (Tabs.Count > 1)
        {
            SelectedTab = Tabs[Tabs.Count - 1];
        }
    }
   }

}

FattureFornitoriViewModel.cs:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace WpfAppFatture.ViewModel
{
  public partial class FattureFornitoriViewModel : ObservableObject
  {
    MainViewModel _vm;

    public FattureFornitoriViewModel(MainViewModel vm)
    {
        _vm = vm;
    }

    [RelayCommand]
    void OpenFattureFornitori()
    {
        ScaricaEmailFatture ff = new();
    }
  }
}

When the line FattureFornitori = serviceProvider.GetRequiredService<MainViewModel>(); is commented out in the constructor of MainViewModel, the MainWindow opens correctly. However, when the line is active, the MainWindow does not open and no error is displayed. I have added a try-catch block to capture any exceptions, but no exception is caught. Does anyone have any idea what might be causing this issue or how to resolve it?

Thank you in advance!

本文标签: