Xamarin Android应用程序

本文关键字:应用程序 Android Xamarin | 更新日期: 2023-09-27 18:37:14

今天开始在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();
        }
    }

这工作正常。但是当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 中引用但它不会触发按钮事件有人知道为什么吗?

Xamarin Android应用程序

您应该开始一个新活动,而不是设置内容视图

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