2016-05-02 108 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它使用一個操作欄和碎片 –