2013-10-29 120 views
1

今天開始在Visual Studio xamarin開發它看起來很好,至今 但我的問題是Xamarin的Android應用程序

如何從點擊按鈕切換到另一個佈局(查看)

當我這樣做對我的登錄頁面如下:

[Activity(Label = "Test", MainLauncher = true, Icon = "@drawable/icon")] 
public class Test: Activity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     SetContentView(Resource.Layout.Afvalwijzer); 

     EditText Bla1 = FindViewById<EditText>(Resource.Id.Bla1); 
     Bla1.Hint = "Your text"; 

     EditText Bla2= FindViewById<EditText>(Resource.Id.Bla2); 
     Bla2.Hint = "Your text"; 

     Button Login = FindViewById<Button>(Resource.Id.BtnLogin); 
     Login.Click += new EventHandler(Login_Click); 

    } 

    void Login_Click(object sender, EventArgs e) 
    { 
     EditText Bla1 = FindViewById<EditText>(Resource.Id.Bla1); 
     EditText Bla2 = FindViewById<EditText>(Resource.Id.Bla2); 

     if (!String.IsNullOrEmpty(Bla1.Text) && !String.IsNullOrEmpty(Bla2.Text)) 
     { 
      AfvalwijzerWS WebS = new AfvalwijzerWS(); 
      var Data = WebS.Authenticate(Bla1.Text, Bla2.Text); 
      TextView txt = FindViewById<TextView>(Resource.Id.ContentTextView); 

      if (Data.Count() > 0) 
      { 
       SetContentView(Resource.Layout.Index); 
      } 
      else 
      { 
       MessageBox("No Access !"); 
      } 
     } 
     else 
     { 
      MessageBox(); 
     } 
    } 

這工作正常。 但是當指數(佈局) IM我有相同的代碼是這樣的:

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

     SetContentView(Resource.Layout.Index); 
     Button Contact = FindViewById<Button>(Resource.Id.BtnContact); 
     Contact.Click += (sender, e) => 
     { 
      SetContentView(Resource.Layout.Contact); 
     }; 

    } 

及其在佈局OFC 的XML引用,但它不會觸發按鈕事件沒有人知道爲什麼嗎?

回答

5

你應該開始一個新的活動,而不是設置的內容視圖

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

     SetContentView(Resource.Layout.Index); 
     Button Contact = FindViewById<Button>(Resource.Id.BtnContact); 
     Contact.Click += (sender, e) => 
     { 
      StartActivity(new Intent(this, typeof(/* whatever activity you want */))); 

      // e.g. 
      //StartActivity(new Intent(this, typeof(SplashActivity))); 
     }; 

    } 
+0

感謝DaveDev將檢查它的明天,如果這個工程我讓你知道並接受的答案它看起來合乎邏輯非常感謝你 –

相關問題