【问题标题】:MVVMCross Back Navigation in VievModel with ShowViewModelMVVM 在 VievModel 中使用 ShowViewModel 进行交叉导航
【发布时间】:2017-06-01 09:13:45
【问题描述】:

我正在使用 MVVMCross 在 Xamarin 中制作应用程序。这是我的代码:

Android View(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 是关于绑定的,这就是我想以这种方式制作它的原因。

【问题讨论】:

  • 要回到之前的 ViewModel,你可以简单地使用Close(this);
  • 我知道。但是我的操作栏上没有后退按钮。我发现我需要删除“OnCreate”中的 SupportActionBar.SetDisplayHomeAsUpEnabled(true) 行(在 iOS 中也是如此)。我不知道这个方法是从哪里来的。在 iOS 上,后退导航有效,但是当我在 Android 中单击后退按钮时没有任何反应:/ 我是否需要覆盖 BackButton 默认方法?

标签: c# xamarin mvvm xamarin.ios xamarin.android


【解决方案1】:

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

更多关于执行命令here的信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    相关资源
    最近更新 更多