2015-01-17 4 views
1

방금 ​​첫 번째 안드로이드 앱을 만들 때 xamarin을 사용하기 시작했으며 작은 문제가 있습니다.Xamarin - 안드로이드 - 스플래쉬 스크린 (복수)

앱 시작 부분에 2 개의 시작 화면을 넣고 싶습니다.

나는 잘 작동하는 첫 번째 작품을 만든 다음 두 번째 작품 인 mainActivity를 만들었습니다.

그러나 어떤 이유로 인해 두 번째 시작 화면이 표시되지 않습니다.

두 번째 줄에서이 줄을 제거하면 작동하지만 mainActivity로 이동하지 않습니다.

StartActivity(typeof(MainActivity)); 

MainActivity.cs

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

      Thread.Sleep(3000); 

      StartActivity(typeof(SplashActivity2)); 
     } 
    }; 

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

      Thread.Sleep(3000); 

      StartActivity(typeof(MainActivity)); 
     } 
    }; 

    [Activity (Label = "coco1_droid", Icon = "@drawable/icon")] 
    public class MainActivity : Activity 
    { 
     int count = 1; 

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

      // Set our view from the "main" layout resource 
      SetContentView (Resource.Layout.Main); 

      // Get our button from the layout resource, 
      // and attach an event to it 
      Button button = FindViewById<Button> (Resource.Id.myButton); 

      button.Click += delegate { 
       button.Text = string.Format ("{0} clicks!", count++); 
      }; 
     } 
    } 

답변

1

문제는 당신이 Thread.Sleep(3000)과 UI가 일시 UI 스레드을 자고하고 새로운 활동이 OnCreate 방법 반환되기 전에 시작되기 때문이다.

새 활동을 시작하기 전에 타이머 (예 : System.Timers.Timer)를 사용하여 3 초를 기다리는 것이 좋습니다. 그런 식으로 UI가 고정되지 않고 OnCreate 메서드가 반환됩니다.

는 내 제안에 당신의 예를 수정했습니다

MainActivity.cs

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

      Timer timer = new Timer(); 
      timer.Interval = 3000; // 3 sec. 
      timer.AutoReset = false; // Do not reset the timer after it's elapsed 
      timer.Elapsed += (object sender, ElapsedEventArgs e) => 
      { 
       StartActivity(typeof(SplashActivity2)); 
      }; 
      timer.Start(); 
     } 
    }; 

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

      Timer timer = new Timer(); 
      timer.Interval = 3000; // 3 sec. 
      timer.AutoReset = false; // Do not reset the timer after it's elapsed 
      timer.Elapsed += (object sender, ElapsedEventArgs e) => 
      { 
       StartActivity(typeof(MainActivity)); 
      }; 
      timer.Start(); 
     } 
    }; 

    [Activity (Label = "coco1_droid", Icon = "@drawable/icon")] 
    public class MainActivity : Activity 
    { 
     int count = 1; 

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

      // Set our view from the "main" layout resource 
      SetContentView (Resource.Layout.Main); 

      // Get our button from the layout resource, 
      // and attach an event to it 
      Button button = FindViewById<Button> (Resource.Id.myButton); 

      button.Click += delegate { 
       button.Text = string.Format ("{0} clicks!", count++); 
      }; 
     } 
    } 
+1

을이 훌륭한 답변입니다. 친구를 공유해 주셔서 감사합니다. @ 지미 -이 대답을 +1하고 대답으로 표시하십시오. –

관련 문제