2015-01-04 75 views
0

我試圖在Xamarin Studio中創建一個splashscreen。用佈局創建SplashScreen Xamarin

我做了以下內容:

  • 創建我的佈局下的splashimage。
  • 創建了一個主題(styles.xml),這樣標題欄就被隱藏了。
  • 創建一個activity設置contentview,然後讓線程休眠。

出於某種原因,沒有工作,我希望你能幫助我在這裏:

SplashScreen.cs(閃屏活動)

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 ....

提前致謝!

回答

4

屏幕是空白的,因爲StartActivityOnCreateView調用Thread.Sleep那麼,你先暫停UI線程(這將導致沒有顯示),然後用StartActivity立即退出活動。

爲了解決這個問題,轉移Thread.Sleep()StartActivity()到後臺線程:

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)); 
    }); 
} 
+0

這實際工作!但是,在啓動屏幕呈現之前,發生1秒的空白屏幕。任何想法如何避免這個空白的屏幕? – acido

+0

我對這1秒空白屏幕的解決方案是爲活動添加樣式(它在'setContentView'之前應用),其背景顏色與我的初始視圖相同。這使得對飛濺佈局的輸入更好 – Jmie