在 MAUI 应用中,我如何在 AppShell 中使用依赖注入?

huangapple go评论154阅读模式
英文:

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&lt;IAuthenticationService, AuthenticationService&gt;();

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&lt;AppShell&gt;();   
builder.Services.AddSingleton&lt;IAuthenticationService,
AuthenticationService&gt;();

在 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&lt;AppShell&gt;();   
builder.Services.AddSingleton&lt;IAuthenticationService,
AuthenticationService&gt;();

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;                               
    }                                                                     
}

huangapple
  • 本文由 发表于 2023年3月9日 23:03:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定