2013-10-29 122 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将检查它的明天,如果这个工程我让你知道并接受的答案它看起来合乎逻辑非常感谢你 –

相关问题