如何在 Xamarin Android 中创建透明 activity?应用主题会使应用程序崩溃

How can I create a transparent activity in Xamarin Android? Applying the theme crashes the app

我创建了一个启动 activity.

的生物认证服务

activity 然后用 StartActivityForResult 开始另一个 activity。

我创建了一种样式,目的是使这两个活动不可见。

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>


[Activity(Label = "BiometricActivity", Theme = "@style/Theme.AppCompat.Translucent")]
public class BiometricActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static event EventHandler<BiometricEventArgs> BiometricEventHandler;
    private readonly int BIOMETRIC_REQUEST = 1;

    protected override void OnCreate(Bundle savedInstanceState)
    {           
        base.OnCreate(savedInstanceState);

        Intent intent = new Intent(Application.Context, typeof(BiometricAuth));            
        StartActivityForResult(intent, BIOMETRIC_REQUEST);
    }
}

[Activity(Label = "BiometricAuth", Theme = "@style/Theme.AppCompat.Translucent")]
public class BiometricAuth : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{        
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetResult(Result.Ok);

        Finish();
    } 
}

当我在 ActivityAttribute 中使用 Theme 定义活动时,应用了样式并且看起来很棒,但是一旦活动完成应用程序就会崩溃并显示 NullReferenceException

[InputMethodManager] prepareNavigationBarInfo() DecorView@9cdfd0[BiometricActivity]
[InputMethodManager] getNavigationBarColor() -855310
[InputMethodManager] prepareNavigationBarInfo() DecorView@9cdfd0[BiometricActivity]
[InputMethodManager] getNavigationBarColor() -855310
[InputMethodManager] Starting input: tba=com.aplicatzia.meklockv2 ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
[InputMethodManager] startInputInner - Id : 0
[InputMethodManager] startInputInner - mService.startInputOrWindowGainedFocus
[InputTransport] Input channel destroyed: 'ClientS', fd=98
[ViewRootImpl@86b53bb[BiometricAuth]] MSG_WINDOW_FOCUS_CHANGED 0 1
[InputMethodManager] prepareNavigationBarInfo() DecorView@e5be706[BiometricAuth]
[InputMethodManager] getNavigationBarColor() -855310
[ViewRootImpl@c984152[MainActivity]] stopped(false) old=false
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'

Worker Thread       System.Diagnostics.Debugger.Mono_UnhandledException_internal()
0xFFFFFFFFFFFFFFFF in System.Diagnostics.Debugger.Mono_UnhandledException_internal
0x1 in System.Diagnostics.Debugger.Mono_UnhandledException at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Diagnostics/Debugger.cs:125,4
0x20 in Android.Runtime.DynamicMethodNameCounter.55
0x6 in Xamarin.Forms.Platform.Android.PlatformConfigurationExtensions.OnThisPlatform<Xamarin.Forms.Application> at D:\a\_work\s\Xamarin.Forms.Platform.Android\PlatformConfigurationExtensions.cs:8,4
0xC in Xamarin.Forms.Platform.Android.AppCompat.FragmentContainer.OnResume at D:\a\_work\s\Xamarin.Forms.Platform.Android\AppCompat\FragmentContainer.cs:126,4
0x8 in AndroidX.Fragment.App.Fragment.n_OnResume at C:\a\_work\s\generated\androidx.fragment.fragment\obj\Release\monoandroid12.0\generated\src\AndroidX.Fragment.App.Fragment.cs:2570,4
0x11 in Android.Runtime.DynamicMethodNameCounter.55

如果我使用 SetTheme 以编程方式应用主题的两个活动,则不会发生崩溃,但背景是黑色而不是透明的。

[Activity(Label = "BiometricActivity")]
public class BiometricActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static event EventHandler<BiometricEventArgs> BiometricEventHandler;
    private readonly int BIOMETRIC_REQUEST = 1;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        SetTheme(Resource.Style.Theme_AppCompat_Translucent);
        base.OnCreate(savedInstanceState);
    }
}

如何为这两个活动创建透明背景?

首先,如果您的项目是xamarin.android项目,我建议您让activity实现AppCompatActivity。如:

public class MainActivity : AndroidX.AppCompat.App.AppCompatActivity{}

而一个forms项目,里面也只有一个MainActivity。我已经在表单项目和 android 项目中测试了您的主题。他们两个都很好。

因此您可以尝试使用它,例如:

[Activity(Label = "BiometricActivity", Theme = "@style/Theme.AppCompat.Translucent")]
public class BiometricActivity : AndroidX.AppCompat.App.AppCompatActivity