【问题标题】:Xamarin how to open another layout by clicking on new resource layoutXamarin 如何通过单击新资源布局打开另一个布局
【发布时间】:2016-05-05 05:38:03
【问题描述】:

我是 Xamarin 的新手,但我正在通过创建停车应用程序进行培训。现在我尝试访问另一个布局时遇到了问题。

这是我的 MainActivity.cs

    [Activity(Label = "CustomActionBarParking", MainLauncher = true, Icon = "@drawable/icon", Theme ="@style/CustomActionBarTheme")]
        public class MainActivity : Activity
        {
    private LinearLayout mBarZone;
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                ActionBar.SetDisplayShowCustomEnabled(true);
                SetContentView(Resource.Layout.action_bar);
                mBarZone = FindViewById<LinearLayout>(Resource.Id.linearLayout2);
                mBarZone.Click += (object sender, EventArgs args) =>
                {
                    SetContentView(Resource.Layout.zones_list);
                };
  }}}

在这里,我通过单击“区域”操作栏从我的菜单访问。并打开“区域列表”布局。

从这里我想通过单击蓝色区域操作栏按钮访问另一个布局:vehicle_not_parked。但我不知道我必须在哪里初始化它,因为当我在 OnCreate 方法的 MainAcitivy 类中初始化它时,我得到了错误,我的对象可以为空。然后我创建如下所示的 ZonesActivity.cs:

[Activity(Label = "CustomActionBarParking")]
    public class ZonesActivity : Activity
    {
        private LinearLayout mBlueZone;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.zones_list);
            mBlueZone = FindViewById<LinearLayout>(Resource.Id.linearLayout2);
            mBlueZone.Click += (object sender, EventArgs args) =>
            {
                SetContentView(Resource.Layout.vehicle_not_parked);

            };

        }}}

但是当我试图在 Main Activity 类中调用这个类时,我必须处理 Bundle savedInstanceState 属性。我真的不知道如何从一个视图 -> 第二个视图然后 -> 第三个视图。

【问题讨论】:

    标签: c# android android-layout xamarin xamarin.android


    【解决方案1】:

    如果我理解正确,您在按钮单击事件中换出布局?我认为最好开始一项新活动

    mBarZone.Click += delegate {
           StartActivity(typeof(ZonesActivity));
    };
    

    Docs on starting a new activity

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 2017-02-25
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多