MvvmCross android 应用程序中的简单 2 屏幕导航中断后退按钮
Simple 2 screen navigation in MvvmCross android app breaks back-button
我使用最新的 MvvmCross 版本 (4.2.3) 制作了一个非常简单的项目,其中我有 2 个 android 屏幕:FirstView 和 SecondView。我使用 FirstView 上的按钮导航到 SecondView:
// 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 设备上使用后退按钮并单击该按钮再次显示 SecondView 时,SecondView 被多次 (5-6) 添加到导航堆栈。我必须多次单击后退按钮才能返回 FirstView。
代码:
public class SecondViewModel : DreamsViewModelBase
{
private readonly IDreamsWebService _webService;
public SecondViewModel(IDreamsWebService webService)
{
_webService = webService;
}
public void Init(SecondViewModelBundle bundle)
{
Log.Log("Initializing with data: " + bundle.Data);
SecondData = bundle.Data;
}
private string _secondData;
public string SecondData
{
get { return _secondData; }
set { SetProperty(ref _secondData, value, "SecondData"); }
}
}
布局:
<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 多次添加到导航堆栈?我什至尝试设置 activity 标志 "SingleTop" 这似乎是一个糟糕的解决方案,甚至没有工作。
在您位于 GitHub 的项目中,您正在 onResume
activity 生命周期回调中调用 PrepareUI
方法。这会在每次 FirstView
恢复时添加(堆叠)相同的绑定(应用程序第一次打开,每次 SecondView
关闭)。因此,您最终会在 Show SecondViewModel 按钮上进行多次绑定,这会导致多次打开视图模型。
没有提供清除绑定的方法,但您可以做其他事情:
- 创建一个新的
BindingSet
对象
- 在
onCreate
方法中调用您的 PrepareUI
我使用最新的 MvvmCross 版本 (4.2.3) 制作了一个非常简单的项目,其中我有 2 个 android 屏幕:FirstView 和 SecondView。我使用 FirstView 上的按钮导航到 SecondView:
// 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 设备上使用后退按钮并单击该按钮再次显示 SecondView 时,SecondView 被多次 (5-6) 添加到导航堆栈。我必须多次单击后退按钮才能返回 FirstView。
代码:
public class SecondViewModel : DreamsViewModelBase
{
private readonly IDreamsWebService _webService;
public SecondViewModel(IDreamsWebService webService)
{
_webService = webService;
}
public void Init(SecondViewModelBundle bundle)
{
Log.Log("Initializing with data: " + bundle.Data);
SecondData = bundle.Data;
}
private string _secondData;
public string SecondData
{
get { return _secondData; }
set { SetProperty(ref _secondData, value, "SecondData"); }
}
}
布局:
<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 多次添加到导航堆栈?我什至尝试设置 activity 标志 "SingleTop" 这似乎是一个糟糕的解决方案,甚至没有工作。
在您位于 GitHub 的项目中,您正在 onResume
activity 生命周期回调中调用 PrepareUI
方法。这会在每次 FirstView
恢复时添加(堆叠)相同的绑定(应用程序第一次打开,每次 SecondView
关闭)。因此,您最终会在 Show SecondViewModel 按钮上进行多次绑定,这会导致多次打开视图模型。
没有提供清除绑定的方法,但您可以做其他事情:
- 创建一个新的
BindingSet
对象 - 在
onCreate
方法中调用您的PrepareUI