我創建了一個啟動活動的生物特征認證服務。
然后該活動使用 開始另一個活動StartActivityForResult。
我創建了一種風格,旨在使這兩種活動都隱形。
<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();
}
}
當我在 中定義活動時Theme,ActivityAttribute樣式已應用并且看起來很棒,但是一旦活動完成,應用程式就會崩潰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\1\s\Xamarin.Forms.Platform.Android\PlatformConfigurationExtensions.cs:8,4
0xC in Xamarin.Forms.Platform.Android.AppCompat.FragmentContainer.OnResume at D:\a\_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\FragmentContainer.cs:126,4
0x8 in AndroidX.Fragment.App.Fragment.n_OnResume at C:\a\_work\1\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);
}
}

我可以做些什么來為這兩個活動創建一個透明的背景?
uj5u.com熱心網友回復:
首先,如果你的專案是 xamarin.android 專案,我建議你讓 Activity 實作 AppCompatActivity。如:
public class MainActivity : AndroidX.AppCompat.App.AppCompatActivity{}
而對于一個表單專案,里面也只有一個 MainActivity。我在表單專案和 android 專案中測驗了你的主題。他們倆都作業得很好。
因此,您可以嘗試使用它,例如:
[Activity(Label = "BiometricActivity", Theme = "@style/Theme.AppCompat.Translucent")]
public class BiometricActivity : AndroidX.AppCompat.App.AppCompatActivity
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/483545.html
