2016-05-02 112 views
1

我是Xamarin的新手,但我正在通过创建停车应用程序进行培训。现在通过尝试访问另一个布局来解决问题。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。但是我不知道我需要初始化它的位置,因为当我在MainAcitivy类的OnCreate方法中初始化它时,那么我得到了错误,我的对象是可空的。然后,我创建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); 

      }; 

     }}} 

但是当我tryed调用这个类中的主要活动类我不得不应对捆绑savedInstanceState财产。我真的不知道如何从一个视图 - >第二视图然后 - >第三视图。

回答

1

如果我正确理解你,你在按钮点击事件中交换布局?我认为这将是最好的开始新的活动

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

Docs on starting a new activity

+0

是我做交换。我必须在哪里开始那项活动?在OnCreate()方法的MainActitivy类中?因为当我初始化这个活动时,当我点击布局操作栏以获取vehicle_not_parked视图时,出现错误。 :System.NullReferenceException:未将对象引用设置为对象的实例 – BinaryTie

+0

查看此示例应用程序https://developer.xamarin.com/guides/android/getting_started/hello,android_multiscreen/hello,android_multiscreen_quickstart/以及应用程序启动通话记录活动https://github.com/xamarin/monodroid-samples/blob/master/PhonewordMultiscreen/Phoneword/MainActivity.cs#L50 –

+0

要么或看看这个https://github.com/ xamarin/monodroid-samples/blob/master/PhonewordMultiscreen/Phoneword/MainActivity.cs#L50它使用一个操作栏和碎片 –