c# - MvvmCross android 应用程序中的简单 2 屏幕导航中断后退按钮

标签 c# android xamarin android-emulator mvvmcross

我使用最新的 MvvmCross 版本 (4.2.3) 制作了一个非常简单的项目,其中我有 2 个安卓屏幕: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 时)添加(堆叠)相同的绑定(bind)。因此,您最终会在 Show SecondViewModel 按钮上进行多次绑定(bind),这会导致多次打开 View 模型。

没有提供清除绑定(bind)的方法,但您可以做其他事情:

  1. 创建一个新的BindingSet对象
  2. onCreate 方法中调用您的 PrepareUI

关于c# - MvvmCross android 应用程序中的简单 2 屏幕导航中断后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39055505/

相关文章:

c# - 我如何在部分类中使用 "override"方法?

java - 如何在另一个 Activity 上膨胀布局对话框?

c# - Angular 2 和 C#/.NET

android - iOS、Android 上的 CSS 网格布局 - 网格模板区域的问题

Android 编辑 Sysctl 设置

ios - NSAttributedString.initWithHTML 在 MonoTouch 中不可用

xamarin - Shared Project 和 PCL 共享代码选哪个?

c# - Xamarin.GooglePlayServices.Ads : how to add a bundle to the ad request

c# - 使用 ASP.NET MVC 创建 LINQ to SQL 数据模型的数据上下文

C#:当您枚举 SortedDictionary 时,它是否已排序?