Xamarin.Android启动页与主页间出现白屏问题求助
Hi there, let's figure out why you're seeing that annoying white screen after the splash screen on your Android app. The root issue here is the window background of your MainActivity showing through while Xamarin.Forms initializes and loads your MainPage. Here's a breakdown of what's happening and how to fix it:
What's Causing the White Screen?
When your SplashActivity launches MainActivity, the MainActivity uses the MainTheme by default. While Xamarin.Forms spins up and renders your MainPage, the MainActivity's window displays whatever background is defined in MainTheme—which is likely white if you haven't customized it. This creates the jarring white flash between the splash screen and your app's first page.
Your previous attempts (adding the splash screen to MainTheme or setting MainActivity's theme to splashscreen) might not have worked because either:
- You didn't switch back to the normal theme after Forms loaded, causing styling issues later
- The theme transition wasn't handled properly during the initialization phase
Step-by-Step Fix
1. Update Your MainTheme to Use the Splash Background Temporarily
First, modify your styles.xml to make MainTheme use your splash screen background initially. This ensures the splash visual stays visible until your MainPage is fully ready:
<style name="MainTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Use the splash screen background to avoid white flash --> <item name="android:windowBackground">@drawable/splash_screen</item> <!-- Keep your existing theme properties --> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> </style>
2. Switch Back to Normal Background in MainActivity
Once Xamarin.Forms has finished loading your app, you can reset the window background to your app's normal background (or transparent, if your MainPage handles the background styling). Update your MainActivity code:
[Activity(Label = "SplahScreenSample", Icon = "@mipmap/icon", LaunchMode = LaunchMode.SingleInstance, Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Initialize Xamarin.Forms and load your app global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); // After loading, reset the window background to your app's default // Replace with your app's specific background color or drawable if needed Window.SetBackgroundDrawableResource(Android.Resource.Color.Transparent); } }
3. Optimize Your SplashActivity (Optional but Clean)
You can simplify your SplashActivity to ensure smoother navigation—there's no need for an empty OnCreate with PersistableBundle:
[Activity(Label = "SplahScreenSample", Icon = "@mipmap/icon", Theme = "@style/splashscreen", MainLauncher = true, NoHistory = true)] public class SplashActivity : AppCompatActivity { protected override void OnResume() { base.OnResume(); // Launch MainActivity as soon as the splash screen is ready StartActivity(new Intent(this, typeof(MainActivity))); } }
Why This Works
By setting MainTheme's window background to your splash screen, you maintain the splash visual while Xamarin.Forms initializes. Once your MainPage is loaded, switching the window background ensures your app's normal styling takes over without any white flashes.
内容的提问来源于stack exchange,提问作者Pelin Konaray

