问题描述
我正在尝试在 Xamarin Studio 中创建启动画面.
Im trying to create a splashscreen in Xamarin Studio.
我做了以下事情:
- 使用初始图像创建我的布局.
- 创建了一个主题 (styles.xml) 以便隐藏标题栏.
- 创建了一个设置 contentview 然后让线程休眠的活动.
由于某种原因它不起作用,我希望你能在这里帮助我:
For some reason it did not work and I hoped you could help me out here:
SplashScreen.cs(SplashScreen 活动)
SplashScreen.cs (SplashScreen activity)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace EvoApp
{
[Activity (MainLauncher = true, NoHistory = true, Theme = "@style/Theme.Splash")]
public class SplashScreen : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
this.SetContentView (Resource.Layout.Splash);
ImageView image = FindViewById<ImageView> (Resource.Id.evolticLogo);
image.SetImageResource (Resource.Drawable.Splash);
Thread.Sleep (2000);
StartActivity (typeof(MainActivity));
}
}
}
styles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
所以结果是一个空白的 SplashActivity....
So the result of this is a blank SplashActivity....
提前致谢!
推荐答案
屏幕是空白的,因为通过调用 Thread.Sleep
然后在 OnCreateView
中调用 StartActivity
code>,您首先暂停 UI 线程(这不会导致任何显示),然后使用 StartActivity
立即退出活动.
The screen is blank because by calling Thread.Sleep
then StartActivity
in OnCreateView
, you are first pausing the UI thread (which will cause nothing to display) and are then exiting the activity immediately by using StartActivity
.
要解决此问题,请将 Thread.Sleep()
和 StartActivity()
移至后台线程:
To fix this, shift Thread.Sleep()
and StartActivity()
into a background thread:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
this.SetContentView (Resource.Layout.Splash);
ImageView image = FindViewById<ImageView> (Resource.Id.evolticLogo);
image.SetImageResource (Resource.Drawable.Splash);
System.Threading.Tasks.Task.Run( () => {
Thread.Sleep (2000);
StartActivity (typeof(MainActivity));
});
}
这篇关于使用布局 Xamarin 创建 SplashScreen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!