2017-06-01 71 views
0

我在Xamarin中使用MVVMCross創建應用程序。這是我的代碼:MVVMCross使用ShowViewModel在VievModel中返回導航

Android設備上查看(iOS版幾乎是相同的):

var button = (Button)FindViewById(Resource.Id.button3); 
var set = this.CreateBindingSet<MenuView, MenuViewModel>(); 
set.Bind(button).To(vm => vm.CommandNavigateToSecondPage); 
set.Apply(); 

核心視圖模型:

public ICommand CommandNavigateToSecondPage 
{ 
    get 
    { 
     return new MvxCommand((() => 
     { 
      ShowViewModel<SecondPageViewModel>(); 
     })); 
    } 
} 

我想有一個默認的後退按鈕將導航我以前頁。我在覈心做了與簡單的功能相同的導航,並有後退按鈕。像這樣:

public void Navigate() 
{ 
    ShowViewModel<SecondPageViewModel>(); 
} 

MVVM是所有關於綁定的,這就是爲什麼我想這樣做。

+0

顯示Android的

[Activity (Label = "MyView")] public class MyView : MvxActivity { protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView(Resource.Layout.ThirdNativeView); ActionBar.SetDisplayHomeAsUpEnabled(true); } /// handle back navigation here public override bool OnOptionsItemSelected(IMenuItem item) { switch (item.ItemId) { case Android.Resource.Id.Home: //Execute viewModel command here this.Finish(); return true; default: return base.OnOptionsItemSelected(item); } } } 

更多信息默認主頁/後退按鈕移到回你可以簡單地使用'關閉(這)之前的視圖模型;' – Fahadsk

+0

我知道。但是我的操作欄上沒有後退按鈕。我發現我需要刪除「OnCreate」中的SupportActionBar.SetDisplayHomeAsUpEnabled(true)行(在iOS中也是如此)。我不知道這個方法來自哪裏。在iOS上,後退導航工作正常,但是當我在Android中單擊後退按鈕時,什麼都沒有發生:/我是否需要重寫BackButton默認方法? – OskarS

回答