无法在共享的Xamarin项目上安装Xamarin.Firebase.Common。

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

Can't install Xamarin.Firebase.Common on shared Xamarin project

问题

Xamarin.Firebase.Common是否应该能够安装到共享的Xamarin项目上?

我尝试安装它,但出现错误:

错误 NU1202:包Xamarin.Firebase.Common 120.1.0.1与netstandard2.0 (.NETStandard,Version=v2.0)不兼容。包Xamarin.Firebase.Common 120.1.0.1支持:

  • monoandroid12.0 (MonoAndroid,Version=v12.0)
  • net6.0-android31.0 (.NETCoreApp,Version=v6.0)

Android项目的目标框架目标Android版本都设置为Android 13

英文:

Is the package Xamarin.Firebase.Common supposed to be able to get installed on the shared Xamarin project?

I try to install it, but I get an error:

> Error NU1202 Package Xamarin.Firebase.Common 120.1.0.1 is not
> compatible with netstandard2.0 (.NETStandard,Version=v2.0). Package
> Xamarin.Firebase.Common 120.1.0.1 supports:
> - monoandroid12.0 (MonoAndroid,Version=v12.0)
> - net6.0-android31.0 (.NETCoreApp,Version=v6.0)

Both Target Framework and Target Android Version are set to Android 13 for the Android project.

答案1

得分: 1

首先,您不能在您的Xamarin.Forms应用的共享项目中使用具有依赖项于Android的Xamarin.Firebase.Common包。

其次,您不能在共享的跨平台上下文中使用原生异常。因此,您将无法在共享项目中访问FirebaseNetworkException等异常。

因此,由于您已经在使用Plugin.FirebaseAuth,您需要捕获FirebaseAuthException,它包装了不同的特定于平台的异常并将它们隐藏在错误代码后面。

您的代码应该类似于以下内容:

try
{
    var provider = new OAuthProvider("google.com");
    var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
}
catch (FirebaseAuthException e)
{
    switch(e.ErrorType)
    {
        case ErrorType.NetWork:
            // 处理网络错误
            break;
        case InvalidCredentials:
            // 处理无效凭据
            break;
        //...
        default: break;
    }
}

有关ErrorType枚举到Firebase异常的映射的完整列表,请参见文档

因此,无论何时需要捕获异常,您都可以检查ErrorType并执行适当的操作或向用户显示错误消息。

英文:

First of all, you cannot use the Xamarin.Firebase.Common package, which has a dependency on Android, in the shared project of your Xamarin.Forms app.

Secondly, you cannot use native exceptions in the shared, cross-platform context. Therefore, you won't have access to FirebaseNetworkException, etc. in the shared project.

Instead, since you're already using the Plugin.FirebaseAuth, you need to catch FirebaseAuthException instead, which wraps the different platform-specific exceptions and hides them behind error codes.

Your code should be looking something like this:

try
{
    var provider = new OAuthProvider("google.com");
    var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
}
catch (FirebaseAuthException e)
{
    switch(e.ErrorType)
    {
        case ErrorType.NetWork:
            //handle network error 
            break;
        case InvalidCredentials:
            //handle invalid credentials
            break;
        //...
        default: break;
    }
}

There's a full list for the mapping of the ErrorType enum to the Firebase exceptions in the documentation.

Therefore, wherever you need to catch an exception, you can check the ErrorType and perform the appropriate operation or show an error message to the user.

答案2

得分: 0

只需按照ewerspej所说,您需要在Xamarin.Forms项目的Android部分安装Xamarin.Firebase.Common包。

然后,您可以在Android部分创建一个类,并在该类的方法中使用FirebaseNetworkException。之后,您可以使用依赖服务在共享的Xamarin项目中调用该方法。

有关更多信息,您可以参考关于Xamarin.Forms DependencyService介绍的官方文档。

英文:

Just as ewerspej said, you need to install the Xamarin.Firebase.Common pakcage in the xamarin.forms project's android part.

And then, you can create a class in the android part and use the FirebaseNetworkException in the class's method. After that, you can use the dependency service to call the method in the shared Xamarin project.

For more information, you can refer to the official document about the Xamarin.Forms DependencyService Introduction.

huangapple
  • 本文由 发表于 2023年2月8日 17:54:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384009.html
匿名

发表评论

匿名网友

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

确定