未注册要在ViewModel中使用的服务
你好,我问这个绝望,因为我找不到一个解决方案。我正在做一个mvvm项目在wpf与依赖注入,我得到了这个类。
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public abstract class ViewModelCore:ObservableObject
{
}
public class NavigationService : ObservableObject, INavigationService
{
private ViewModelCore _currentView;
private Func<Type, ViewModelCore> _viewModelFactory;
public ViewModelCore CurrentView
{
get => _currentView;
private set
{
_currentView = value;
OnPropertyChanged();
}
}
public NavigationService(Func<Type,ViewModelCore> viewModelFactory)
{
_viewModelFactory = viewModelFactory;
}
public void NavigateTo<TViewModel>() where TViewModel : ViewModelCore
{
ViewModelCore viewModel = _viewModelFactory.Invoke(typeof(TViewModel));
CurrentView = viewModel;
}
}
public class MainViewModel:ViewModelCore
{
private INavigationService _navigation;
public INavigationService Navigation
{
get => _navigation;
set
{
_navigation = value;
OnPropertyChanged();
}
}
public RelayCommand NavigationHomeCommand { get; set; }
public RelayCommand NavigationPatientListViewCommand { get; set; }
public RelayCommand NavigationAppointmentsViewCommand { get; set; }
public MainViewModel(INavigationService navService)
{
Navigation = navService;
NavigationHomeCommand = new RelayCommand(o => true, o => { Navigation.NavigateTo<HomeViewModel>(); });
NavigationPatientListViewCommand = new RelayCommand(o => true, o => { Navigation.NavigateTo<PatientListViewModel>(); });
NavigationAppointmentsViewCommand = new RelayCommand(o => true, o => { Navigation.NavigateTo<AppointmentViewModel>(); });
}
}
public partial class App : Application
{
private ServiceProvider _serviceProvider;
public App()
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton(
provider => new MainWindow
{
DataContext = provider.GetRequiredService<MainViewModel>()
}
);
services.AddSingleton<MainViewModel>();
services.AddSingleton<HomeViewModel>();
services.AddSingleton<PatientListViewModel>();
services.AddSingleton<AppointmentViewModel>();
services.AddScoped<INavigationService, NavigationService>();
services.AddScoped<IAppointmentService, AppointmentService>();
services.AddSingleton<Func<Type, ViewModelCore>>(serviceProvider => viewModelType => (ViewModelCore)serviceProvider.GetRequiredService(viewModelType));
_serviceProvider = services.BuildServiceProvider();
}
protected override void OnStartup(StartupEventArgs e)
{
var mainWindow = _serviceProvider.GetService<MainWindow>();
mainWindow.Show();
base.OnStartup(e);
}
我得到这个错误消息:System.InvalidOperationException:“在尝试激活“Healthplss.Services. AppointmentService”时,无法解析类型“Healthplss. Services.GenericDataService”“1[Healthplss.Models.Appointment]”的服务。“尽管该服务在我的app.xaml.cs中重新注册,但我也在约会构造器视图模型中声明了要访问的DI容器,但它不允许我访问。”有人知道吗?先谢谢你。
Edit1:添加AppointmentService
public class AppointmentService:IAppointmentService
{
private readonly GenericDataService<Appointment> _genericDataService;
public AppointmentService(GenericDataService<Appointment> genericDataService)
{
_genericDataService = genericDataService;
}
public async Task AddAppointmentAsync(Models.Appointment appointment)
{
await _genericDataService.Create(appointment);
}
public Task DeleteAppointmentAsync(int id)
{
throw new NotImplementedException();
}
public Task GetAppointmentById(int id)
{
throw new NotImplementedException();
}
public async Task<List<Models.Appointment>> GetAppointmentsAsync()
{
return await _genericDataService.GetAll();
}
public Task UpdateAppointmentAsync(Models.Appointment appointment)
{
throw new NotImplementedException();
}
}
我想在数据库中写入实体,所以我使用通用数据服务
1条答案
按热度按时间dzhpxtsq1#
查看您的DI注册,我没有看到
GenericDataService
类被注册。假设,基于这个类的命名空间,这是你自己的类,你也需要注册它。也许你想分配一些接口,例如。
IAppointmentDataService
到GenericDataService<Appointment>
,并像注册其他类一样注册它:如果类
GenericDataService
具有带参数的构造函数,您还需要确保它的构造函数中的所有类型都已注册