2015-11-18 98 views
3

我正在创建一个显示启动画面并创建主要活动的应用程序。我下面这个教程,似乎相当直截了当:https://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/Xamarin中的启动画面太慢Android

实现,我可以顺利看到启动后,但也有一些次(1比20),其与S5我看到以下画面:

Wrong screen

其次(右)飞溅(从仿真器中获得,但只是为了让我的观点):

enter image description here

所以我的猜测因为有时Xamarin花了很长时间来加载应用程序,因此它有一个延迟显示飞溅。有什么办法可以防止这种情况发生?

更新1 我跟着教程,但我已经删除了睡眠这个:

Insights.Initialize ("<APP_KEY>", Application.Context); 
StartActivity(typeof (MainActivity)); 
+0

您是否在splash主题中设置了正确的图像,并为该活动设置了主题,如教程中所示?它看起来像你的活动没有windowBackground图像。你有没有检查过其他版本的Android? – dylansturg

+0

是的,我已经设置了一切像教程和19 20年它看起来不错,只有20出现在第一屏不到一秒钟,然后显示第二个与正确的布局。 –

+1

[Xamarin Splash屏幕示例可能在手机横向模式下不起作用。如何解决它?](http://stackoverflow.com/questions/29991174/xamarin-splash-screen-example-does-not-work-in-landscape-mode-on-a-phone-how-to) – matthewrdev

回答

3

的例子调用UI线程上的Thread.Sleep(10000); ...... 这将锁住应用程序并生成一个ANR

修复它由backgrounding睡眠,然后触发下一个活动:

namespace SplashScreen 
{ 
    using System.Threading; 

    using Android.App; 
    using Android.OS; 

    [Activity(Theme = "@style/Theme.Splash", MainLauncher = true, NoHistory = true)] 
    public class SplashActivity : Activity 
    { 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      Task.Run (() => { 
       Thread.Sleep (10000); // Simulate a long loading process on app startup. 
       RunOnUiThread (() => { 
        StartActivity (typeof(Activity1)); 
       }); 
      }); 
     } 
    } 
} 
+0

感谢您的答案,但我没有使用睡眠,我更新了我的问题,但我认为你的反应仍然适用。我会及时向大家发布 –

0

即使这个职位是比较旧的,我实现闪屏,并可能通过更新样式解决这个时候也有过类似的经历/ SplashScreen的主题。 @frederico m rinaldi有时看到的屏幕通常是使用Android的默认(Holo)主题创建的。

虽然您没有提供适用于SplashScreen的样式(请参阅accepted answerTheme = @style/Theme.Splash),但这是我的。也许你可以检查它们是否有所不同。

<style name="Theme.Splash" parent ="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Use a fully opaque color as background. --> 
    <item name="android:windowBackground">@android:color/black</item> 
    <!-- This removes the title bar seen within the first screen. --> 
    <item name="windowNoTitle">true</item> 
    <!-- Let the splash screen use the entire screen space by enabling full screen mode --> 
    <item name="android:windowFullscreen">true</item> 
    <!-- Hide the ActionBar (Might be already defined within the parent theme) --> 
    <item name="windowActionBar">false</item> 
</style> 

您可能注意到,我只是用黑色作为背景,因为我的闪屏使用自定义布局文件,而不是静态图像(this.SetContentView(Resource.Layout.SplashScreen);)。此外,加载图像(drawable)可能需要一些时间,这可能是您可以看到默认主题而不是启动屏幕的主要原因。 此外,我省略了android: XML名称空间的某些属性,这是由于Android支持库功能的Google's internal implementation

请注意,为了使用程序兼容性的主题,你的应用程序必须包括AppCompat support library和你的活动必须继承Android.Support.V7.App.AppCompatActivity