不能在.NET MAUI项目中引用平台特定的服务。

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

Cannot reference platform-specific services in .NET MAUI project

问题

I am working on a .NET MAUI application, and I am having trouble referencing platform-specific services in my main project. I have created an interface IFirebaseAuthService in the DisApp24.Services namespace, and I have implemented this interface in the FirebaseAuthService_Android and FirebaseAuthService_iOS classes within their respective Platforms\Android\Services and Platforms\iOS\Services folders.

However, when I try to register these platform-specific services in the MauiProgram.cs file, the compiler cannot find the platform-specific classes.

Here is my MauiProgram.cs code:

using DisApp24.Services;
using Microsoft.Extensions.DependencyInjection;

namespace DisApp24
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();

            // Register platform-specific services.
#if __ANDROID__
            builder.Services.AddSingleton<IFirebaseAuthService, DisApp24.Services.FirebaseAuthService_Android>();
#elif __IOS__
            builder.Services.AddSingleton<IFirebaseAuthService, DisApp24.Services.FirebaseAuthService_iOS>();
#endif

            // ...other configurations...

            return builder.Build();
        }
    }
}

I have tried the following approaches to fix this issue:

  • Changing the namespaces of the platform-specific implementations to match the main project's namespace.

  • Manually adding references to the platform-specific implementations in the .csproj file of the main project.

  • Creating a completely new Project where I reproduced the Code, but the error stays the same

Unfortunately, none of these solutions worked, and the issue persists. It seems that the main project cannot reference the Android or iOS projects for some reason. How can I correctly reference and register these platform-specific services in my .NET MAUI application?

英文:

I am working on a .NET MAUI application, and I am having trouble referencing platform-specific services in my main project. I have created an interface IFirebaseAuthService in the DisApp24.Services namespace, and I have implemented this interface in the FirebaseAuthService_Android and FirebaseAuthService_iOS classes within their respective Platforms\Android\Services and Platforms\iOS\Services folders.

However, when I try to register these platform-specific services in the MauiProgram.cs file, the compiler cannot find the platform-specific classes.

Here is my MauiProgram.cs code:

using DisApp24.Services;
using Microsoft.Extensions.DependencyInjection;

namespace DisApp24
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();

            // Register platform-specific services.
#if __ANDROID__
            builder.Services.AddSingleton&lt;IFirebaseAuthService, DisApp24.Services.FirebaseAuthService_Android&gt;();
#elif __IOS__
            builder.Services.AddSingleton&lt;IFirebaseAuthService, DisApp24.Services.FirebaseAuthService_iOS&gt;();
#endif

            // ...other configurations...

            return builder.Build();
        }
    }
}

I have tried the following approaches to fix this issue:

  • Changing the namespaces of the platform-specific implementations to match the main projects namespace.

  • Manually adding references to the platform-specific implementations in the .csproj file of the main project.

  • Creating a completely new Project where I reproduced the Code, but the error stays the same

Unfortunately, none of these solutions worked, and the issue persists. It seems that the main project cannot reference the Android or iOS projects for some reason. How can I correctly reference and register these platform-specific services in my .NET MAUI application?

答案1

得分: 0

关于在.NET MAUI 项目中引用特定于平台的服务:

方式1:
您可以参考官方文档:调用平台代码。它还提供了代码示例:InvokePlatformCodeDemos

方式2:
我搜索了关于这个问题的信息,并找到了 Gerald Versluis 制作的视频:在.NET MAUI中使用依赖注入的平台特定代码。他提供的解决方案简洁明了。

MauiAppBuilder.Services.AddTransient&lt;TService, TImplementation&gt;(IServiceCollection)

并将服务放置在特定平台的相同命名空间下:

namespace yourProjectName.Platforms
英文:

About reference platform-specific services in .NET MAUI project:

Way1 :

You can refer to official doc: Invoke platform code. It also provides code sample: InvokePlatformCodeDemos.

Way2 :

I searched for information about this and found this video by Gerald Versluis: Platform-Specific Code in .NET MAUI Using Dependency Injection. The solution he provides is concise and clear.

MauiAppBuilder.Services.AddTransient&lt;TService, TImplementation&gt;(IServiceCollection)

And it puts services on a specific platform under the same namespace:

namespace yourProjectName.Platforms

huangapple
  • 本文由 发表于 2023年4月10日 20:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976918.html
匿名

发表评论

匿名网友

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

确定