admin 管理员组文章数量: 1086019
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!
本文标签:
版权声明:本文标题:WPF - Dependency Injection Issue in WPF: MainWindow Not Opening When Resolving FattureFornitoriViewModel - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744091962a2532200.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论