问题描述
我正在创建一个显示启动画面的应用程序,然后创建主要活动.我正在关注本教程,看起来很简单:
接着是(右)飞溅(取自模拟器,但只是为了说明我的观点):
所以我的猜测是,有时 Xamarin 加载应用程序需要很长时间,因此它会延迟显示启动画面.有什么办法可以防止吗?
更新 1我已经按照教程进行操作,但为此我取消了睡眠:
Insights.Initialize ("", Application.Context);StartActivity(typeof(MainActivity));
该示例在 UI 线程上调用 Thread.Sleep(10000);
... 这将锁定应用并生成ANR!
通过将睡眠作为后台然后触发下一个活动来修复它:
命名空间 SplashScreen{使用 System.Threading;使用 Android.App;使用Android.OS;[Activity(Theme = "@style/Theme.Splash", MainLauncher = true, NoHistory = true)]公共类 SplashActivity : 活动{protected override void OnCreate(Bundle bundle){base.OnCreate(捆绑);Task.Run (() => {线程.睡眠(10000);//模拟应用启动时的长时间加载过程.RunOnUiThread (() => {开始活动(typeof(Activity1));});});}}}
I'm creating an app that shows a splash screen and then creates the main activity. I'm following this tutorial that seems pretty straight forward: https://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/
After implementing that I can successfully see the splash but there are some times (1 out of 20) that with an S5 I see the following screen:
Followed by the (right) splash (taken from the Emulator but just to make my point):
So my guess would be that sometimes Xamarin is taking to long to load the app therefore it has a delay showing the splash. Is there any way to prevent that?
UPDATE 1I've followed the tutorial but I've removed the sleep for this:
Insights.Initialize ("<APP_KEY>", Application.Context);
StartActivity(typeof (MainActivity));
The example invokes the Thread.Sleep(10000);
on the UI thread... This will lock up the app and generate an ANR!
Fix it by backgrounding the sleep and then triggering the next activity:
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));
});
});
}
}
}
这篇关于Xamarin Android 中的启动画面太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!