简体   繁体   English

Xamarin Android应用程序

[英]Xamarin Android application

Started today to develop with xamarin in Visual studio it looks nice so far But my question is 今天开始使用Visual Studio中的xamarin开发,到目前为止看起来还不错,但是我的问题是

how to switch from button click to another Layout(View) 如何从单击按钮切换到另一个布局(视图)

When i do this on my login page looks as follow: 当我在登录页面上执行以下操作时:

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

This Works fine. 这很好。 But when im on Index(Layout) i have the same code like this: 但是当我在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);
        };

    }

and its referenced in the xml of the layout ofc but it wont trigger the button event does someone know why ? 并且它在布局ofc的xml中引用,但是它不会触发按钮事件,有人知道为什么吗?

You should be starting a new activity, rather than setting the content view 您应该开始一个新的活动,而不是设置内容视图

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM