英文:
In MAUI app, how do I Use Dependency Injection in AppShell
问题
I would like to use a service I created (with the interface IAuthenticationService) inside the AppShell of a MAUI app (AppShell.xaml.cs).
In other words to do something like this:
public partial class AppShell : Shell
private readonly IAuthenticationService _AuthenticationService;
public AppShell(AuthenticationService authenticationService)
{
InitializeComponent();
_AuthenticationService = authenticationService;
}
For the above I added the following code to the App.xaml.cs file
public partial class App : Application
{
public App(AuthenticationService authenticationService)
{
InitializeComponent();
MainPage = new AppShell(authenticationService);
}
}
But when I run this, I get the error:
> Unable to resolve service for type 'MyApp.Services.AuthenticationService' while attempting to activate 'MyApp.App'.
Needless to say that I have done the code need in MauiProgram.cs
builder.Services.AddSingleton<IAuthenticationService, AuthenticationService>();
So basically how do I do DI in App.xaml.cs or in AppShell.xaml.cs?
英文:
I would like to use a service I created (with the interface IAuthenticationService ) inside the AppShell of a MAUI app (AppShell.xaml.cs).
In other words to do something like this:
public partial class AppShell : Shell
private readonly IAuthenticationService _AuthenticationService;
public AppShell(AuthenticationService authenticationService)
{
InitializeComponent();
_AuthenticationService = authenticationService;
}
For the above I added the following code to the App.xaml.cs file
public partial class App : Application
{
public App(AuthenticationService authenticationService)
{
InitializeComponent();
MainPage = new AppShell(authenticationService);
}
}
But when I run this, I get the error:
> Unable to resolve service for type 'MyApp.Services.AuthenticationService' while attempting to activate 'MyApp.App'.'
Needless to say that I have done the code need in MauiProgram.cs
builder.Services.AddSingleton<IAuthenticationService, AuthenticationService>();
So basically how do I do DI in App.xaml.cs or in AppShell.xaml.cs?
答案1
得分: 2
你传递的是实现而不是接口,因此依赖项容器无法解析依赖关系。
你的代码应该像这样:
private readonly IAuthenticationService _AuthenticationService;
//请注意接口
public AppShell(IAuthenticationService authenticationService)
{
InitializeComponent();
_AuthenticationService = authenticationService;
}
和
public partial class App : Application
{
//请注意接口
public App(IAuthenticationService authenticationService)
{
InitializeComponent();
MainPage = new AppShell(authenticationService);
}
}
英文:
You're passing in the implementation instead of the interface, which is why the dependency cannot get resolved by the dependency container.
Your code should look like this instead:
private readonly IAuthenticationService _AuthenticationService;
//note the interface
public AppShell(IAuthenticationService authenticationService)
{
InitializeComponent();
_AuthenticationService = authenticationService;
}
and
public partial class App : Application
{
//note the interface
public App(IAuthenticationService authenticationService)
{
InitializeComponent();
MainPage = new AppShell(authenticationService);
}
}
答案2
得分: 2
在 MauiProgram.cs 中注册:
builder.Services.AddSingleton<AppShell>();
builder.Services.AddSingleton<IAuthenticationService,
AuthenticationService>();
在 App 类中:
public partial class App : Application
{
public App(AppShell appShell)
{
InitializeComponent();
MainPage = appShell;
}
}
最后在 AppShell 类中:
public partial class AppShell : Shell
{
private readonly IAuthenticationService _authenticationService;
public AppShell(IAuthenticationService authenticationService)
{
InitializeComponent();
_authenticationService = authenticationService;
}
}
英文:
In MauiProgram.cs register:
builder.Services.AddSingleton<AppShell>();
builder.Services.AddSingleton<IAuthenticationService,
AuthenticationService>();
In the App class:
public partial class App : Application
{
public App(AppShell appShell)
{
InitializeComponent();
MainPage = appShell;
}
}
Finally in the AppShell class
public partial class AppShell : Shell
{
private readonly IAuthenticationService _authenticationService;
public AppShell(IAuthenticationService authenticationService)
{
InitializeComponent();
_authenticationService = authenticationService;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论