1

今天开始在 Visual Studio 中使用 xamarin 进行开发,到目前为止它看起来不错但我的问题是

如何从按钮单击切换到另一个布局(视图)

当我在我的登录页面上执行此操作时,如下所示:

[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();
        }
    }

这工作正常。但是当我在 Index(Layout) 上时,我有相同的代码,如下所示:

 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 中被引用,但它不会触发按钮事件有人知道为什么吗?

4

1 回答 1

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

    }
于 2013-10-29T15:24:01.250 回答