0

Xamarin,我有一个简单的Activity加载与另一个Activity加载前五秒钟的等待。图片将不会显示在内容视图

第一个Activity的图像作为Layout的一部分,但此图像未显示。

这里是我的Activity代码:

protected override void OnCreate (Bundle bundle) 
{ 
    base.OnCreate (bundle); 

    SetContentView (Resource.Layout.SplashActivity); 

    Thread.Sleep(5000); 

    var testActivity = new Intent (this, typeof(TestStartup)); 
    StartActivity (testActivity); 
} 

这里是我的SplashActivity布局代码:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/background_color"> 
    <ImageView 
     android:src="@drawable/Splash" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/SplashImage" 
     android:layout_gravity="center_vertical" /> 
</LinearLayout> 

的应用程序等待五秒钟,然后TestStartup Activity负荷,但是,图像不在五秒钟等待期间显示。如何在Activity等待5秒钟时显示此图像?

我可以请这个代码帮忙吗?

在此先感谢

回答

0

它没有显示,因为与

Thread.Sleep(5000); 

你应该摆脱这种逻辑的阻塞UI线程。依靠x secs它总是错误的。您正在等待的计算可能会在之前做好准备,使剩余的等待时间过长,或者可能需要更多的x secs。至"fix"你的问题,你可以使用Handler.postDelayed(Runnable, 5000),并把你需要执行的逻辑内run方法。这样一来,至少,在UI线程不会被阻塞