2016-04-28 24 views
0

本週我一直在學習C#,並即時創建一個應用程序。我試過goto聲明,但是lebels是「超出範圍」我希望它把我送回到原始佈局,我發現我需要回到代碼的頂部。 thx提前。Android C#xamarin幫助!需要按鈕帶我回到代碼頂部

  namespace TheAppOfJack 
      { 
      [Activity(Label = "The App Of Jack", MainLauncher = true, Icon = "@drawable/icon")] 
      public class MainActivity : Activity 
      { 

      int count = 1; 

      protected override void OnCreate(Bundle bundle) 
      { 

      base.OnCreate(bundle); 

      SetContentView(Resource.Layout.Main); 

      Button button1 = FindViewById<Button>(Resource.Id.MyButton); 
      button1.Click += delegate { button1.Text = string.Format("You have clicked this random button {0} times (͡° ͜ʖ ͡°)", count++); }; 

      Button button2 = FindViewById<Button>(Resource.Id.button1); 
      button2.Click += delegate 
      { 
      SetContentView(Resource.Layout.Gallery); 
      Button button3 = FindViewById<Button>(Resource.Id.back); 
      button3.Click += delegate { SetContentView(Resource.Layout.Main); }; 

      Button button4 = FindViewById<Button>(Resource.Id.next1); 
      button4.Click += delegate 
      { 
      SetContentView(Resource.Layout.Gallery2); 
      button3.Click += delegate { SetContentView(Resource.Layout.Main); }; 
      Button button5 = FindViewById<Button>(Resource.Id.next2); 
      button5.Click += delegate 
      { 

      SetContentView(Resource.Layout.Gallery3); 
      Button button6 = FindViewById<Button>(Resource.Id.home); 
      button6.Click += delegate { 

      //this is where i want the code to send me back to the top }; 
      }; 
      }; 
      }; 
      } 
      } 
      } 
+1

你想完成什麼? (也就是說爲什麼你要回到頂端?)一個'for'循環會做你需要的嗎? –

+0

我需要到達代碼的頂部,因爲如果我只是將其設置回郵件佈局,則沒有任何的代碼可以工作。如果這可以通過一個for循環來解決,你可以把它放到我的代碼中,因爲我從未使用它們,也不知道從哪裏開始。 thx –

+0

我認爲你所要做的與Android體系結構非常相反。我認爲你應該找到與你想要做的和研究相似的樣本項目。 –

回答

0

您嘗試的方式並不是Android的工作方式。您無法在「佈局」之間導航,而是在具有與其關聯的佈局的活動之間進行導航。此導航使用StartActivity()方法完成。

MainActivity是您的應用啓動時將顯示的第一項活動。它的關聯佈局(在這裏命名爲Main)應該只有一個按鈕,可以將您帶到下一個屏幕。假設它的ID是btnForActivity2。爲MainActivityOnCreate將是這樣的:

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

    SetContentView(Resource.Layout.Main); 

    Button btn = FindViewById<Button>(Resource.Id.btnForActivity2); 
    btn.Click += delegate 
    { 
     StartActivity (typeof(MyActivity2)); 
    }; 
} 

MyActivity2類,假設你想有兩個按鈕,帶你到新的活動。假設一項活動名爲MyActivity3,另一項名爲MyActivity4。在這種情況下,您的MyActivity2OnCreate方法是這樣的:

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

    SetContentView(Resource.Layout.MyActivity2Layout); 

    Button btn = FindViewById<Button>(Resource.Id.btnForActivity3); 
    btn.Click += delegate 
    { 
     StartActivity (typeof(MyActivity3)); 
    }; 

    btn = FindViewById<Button>(Resource.Id.btnForActivity4); 
    btn.Click += delegate 
    { 
     StartActivity (typeof(MyActivity4)); 
    }; 
} 

這是多麼基本的導航Android版。

順便說一下,您可能已經注意到我沒有提到Back按鈕。這是因爲我從來沒有明確地實現它,因爲與iOS設備不同,Android設備專門爲此設計了一個按鈕。但是,如果您必須有一個可讓您回到上一個活動的按鈕,則只需撥打this.Finish();就足夠了。一旦您爲某個活動調用Finish(),它將從活動堆棧頂部移除,並且以下活動將變爲可見。

+0

Thx,這有助於加載,我覺得有一個網站顯示了這個基本知識的湖泊 –

+0

我很高興我可以提供幫助,也許你可以從這裏開始:https://developer.xamarin.com/guides/android/getting_started/hello,android_multiscreen/ –