如何使用 Xamarin 和 MVVMCross 导航备份/向上



我问这个问题是为了为自己创建记录,我通过环顾四周找到了答案,但我觉得我花了太长时间才弄清楚这一点。假设我在搜索中使用了错误的关键字,我正在尝试将我在搜索中使用的关键字链接到此问题,并希望对其他人有所帮助。

我试图使用 OnOptionsItemSelected 方法导航回上一个活动。这就是Android允许您收听操作栏按钮按下的方式。

public override bool OnOptionsItemSelected(IMenuItem item)
{
    if (item.ItemId != global::Android.Resource.Id.Home) return base.OnOptionsItemSelected(item);
    ViewModel.BackNavigationButtonCommand.Execute();
    return true;
}

我通过以下方式启用后退按钮:

SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);

但是,SetDisplayHomeAsUpEnabled 回调没有转到 OnOptionsItemSelected。

此解决方案的答案是使用 activity 属性:

[Activity(
    Label = "Project Wavelength"
    , Icon = "@drawable/icon"
    , Theme = "@style/Theme.AppCompat.Light.DarkActionBar"
    , ScreenOrientation = ScreenOrientation.Portrait
    , LaunchMode = LaunchMode.SingleTop)]
LaunchMode

= LaunchMode.SingleTop,用于返回到后退堆栈中的现有活动。

[Activity(Label = "DevicesWithSensorsForRoom"
    , Icon = "@drawable/icon"
    , ScreenOrientation = ScreenOrientation.Portrait
    , Theme = "@style/Theme.AppCompat.Light.DarkActionBar"
    , ParentActivity = typeof(RoomsOverviewActivity)
    , LaunchMode = LaunchMode.SingleTop)]

ParentActivity = typeof(RoomsOverviewActivity(,用于定义要返回到的父级。

我不知道是否有使用视图模型的 MVVMCross 解决方案。但是,这现在对我有用。如果有MVVMCross解决方案,我想听听。

OnOptionsItemSelected 和 SetDisplayHomeAsUpEnabled 对于这些属性不是必需的。

最新更新