C# MvvmCross android应用程序中的简单两屏导航会断开“后退”按钮
我使用最新的MvvmCross版本(4.2.3)做了一个非常简单的项目,其中我有两个android屏幕:FirstView和SecondView。我使用FirstView上的按钮导航到SecondView:C# MvvmCross android应用程序中的简单两屏导航会断开“后退”按钮,c#,android,xamarin,android-emulator,mvvmcross,C#,Android,Xamarin,Android Emulator,Mvvmcross,我使用最新的MvvmCross版本(4.2.3)做了一个非常简单的项目,其中我有两个android屏幕:FirstView和SecondView。我使用FirstView上的按钮导航到SecondView: // FirstView.cs private void PrepareShowSecondButton() { var hero = FindViewById<Button>(Resource.Id.firstview_show_second_button);
// FirstView.cs
private void PrepareShowSecondButton()
{
var hero = FindViewById<Button>(Resource.Id.firstview_show_second_button);
hero.Text = DreamsResources.FirstView_ShowSecond_Button_Label;
BindingSet.Bind(hero).To(vm => vm.ShowSecondCommand);
BindingSet.Apply();
}
// FirstViewModel.cs
private MvxCommand _showSecondCommand;
public MvxCommand ShowSecondCommand
{
get
{
_showSecondCommand = _showSecondCommand ?? new MvxCommand(DoShowSecondCommand);
return _showSecondCommand;
}
}
private void DoShowSecondCommand()
{
ShowViewModel<SecondViewModel>(new SecondViewModelBundle() { Data = "Hello from FirstView - " + Hello });
}
布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_firstview_top_container_coordinatorlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:layout_scrollFlags="scroll|enterAlways"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<LinearLayout
android:id="@+id/firstview_content_container_linearlayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
<EditText
android:id="@+id/firstview_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp" />
<TextView
android:id="@+id/firstview_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp" />
<Button
android:id="@+id/firstview_show_second_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
如何避免将SecondView多次添加到导航堆栈中?我甚至尝试过设置活动标志“SingleTop”,这似乎是一个糟糕的解决方案,甚至没有起作用。在GitHub的项目中,您正在调用
onResume
活动生命周期回调中的PrepareUI
方法。这会在每次恢复FirstView
时添加(堆叠)相同的绑定(第一次打开应用程序时,每次关闭SecondView
)。因此,在ShowSecondViewModel按钮上会有多个绑定,这会导致多次打开viewmodel
没有提供清除绑定的方法,但您可以执行其他操作:
BindingSet
对象onCreate
方法中调用您的PrepareUI
代码在我看来没有错。我唯一能想到的是,您多次调用
PrepareShowSecondButton
,复制绑定。这将打开第二个视图,打开次数与应用于“hero”按钮的绑定数量相同。您可以显示完整的FirstView
和FirstViewModel
?
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_firstview_top_container_coordinatorlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:layout_scrollFlags="scroll|enterAlways"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<LinearLayout
android:id="@+id/firstview_content_container_linearlayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
<EditText
android:id="@+id/firstview_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp" />
<TextView
android:id="@+id/firstview_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp" />
<Button
android:id="@+id/firstview_show_second_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>