如何使用MvxCachingFragmentActivity的示例 [英] Example of how to use MvxCachingFragmentActivity

查看:100
本文介绍了如何使用MvxCachingFragmentActivity的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新-感谢@ Martijn00和@ Plac3Hold3r,我设法更新了我的应用程序以使用MvxCachingFragmentCompatActivity,但它无法正常工作.我发现如果我返回,有时ViewModel将可用,但是视图模型中的命令将为null.

Update - Thanks to @Martijn00 and @Plac3Hold3r I have managed to update my app to use the MvxCachingFragmentCompatActivity but it is just not working correctly. I am finding that if I go back sometimes the the ViewModel will be available, but the commands in the view model will be null.

如果我后退然后前进,某些按钮将不响应.我认为这是同样的问题.

Also if I go back and then forward, some of the buttons don't respond. I assume this is the same issue.

我真正需要知道的是缓存活动为我提供了哪些附加功能,以及如何正确使用它.

What I really need to know is what the additional functionality the caching activity gives me, and how to use it properly.

随后出现原始问题...

Original question follows...

我遇到了一个问题,其中片段的视图模型为null.我怀疑打开照相机活动时我的活动已被清除.对于我所有的应用程序,我都使用一个活动,而我的所有视图都是片段.

I am hitting a problem where the view model for a fragment is null. I suspect my activity is being cleared up when I open a camera activity. For all my application am using a single activity and all my views are fragments.

完成摄像机活动后,将重新构建活动,但是片段视图模型之一为空.我目前正在为单个活动使用AppCompatActivity,但通过研究,我可能应该使用MvxCachingFragmentActivity.问题是我不知道该如何使用它.我在任何地方都找不到清晰的解释.

When the camera activity is complete, the activity is reconstructed, but one of the fragments view model is null. I am currently using an AppCompatActivity for my single activity, but through my research I should probably be using a MvxCachingFragmentActivity. The problem is I have no idea how I am supposed to use it. I cannot find a clear explanation anywhere.

有人知道如何使用MvxCachingFragmentActivity或MvxCachingFragmentCompatActivity.

Has anyone got a working example of how to use the MvxCachingFragmentActivity or the MvxCachingFragmentCompatActivity.

我找不到任何可以告诉我如何使用它的地方.

I can't find anywhere where it tells me how I should use it.

我找到了另一个链接示例,但我认为它已经过时了,另一个在此示例中给出的链接是404.

I found this other link example but I think it is out of date and the other link given in this example is a 404.

如果有人知道一个简单的示例,以及该示例是否可以在单个活动中使用,请告诉我.

If anyone knows of a simple sample and whether this will work with a single activity please let me know.

谢谢

推荐答案

设置

MainActivity

MainActivity

创建一个从MvxCachingFragmentCompatActivityMvxCachingFragmentActivity继承的活动.

Create an activity that inherits from MvxCachingFragmentCompatActivity or MvxCachingFragmentActivity.

[Activity]
public class MainActivity : BaseFragmentActivity<MainContainerViewModel>
{
}

MainContainerViewModel

MainContainerViewModel

创建一个Viewmodel来关联到Activity.您将永远不会直接使用此Viewmodel进行导航.相反,您将通过将MainContainerViewModel指定为其父级的Viewmodel的片段导航到该Viewmodel.

Create a Viewmodel to associate to the Activity. You will never navigate directly using this Viewmodel. Instead you will navigate to this Viewmodel via the fragments that specifies MainContainerViewModel as their parent's Viewmodel.

public class MainContainerViewModel : MvxViewModel
{
}

XML布局示例

XML layout example

将布局添加到您的MainActivity.确保包括具有ID的FrameLayout.在这种情况下,content_frame.此ID很重要,因为这是您将如何识别片段放置位置的框架.如果您要为同一视图添加多个片段,则可以指定多个FrameLayout's.

Add a layout to your MainActivity. Make sure to include a FrameLayout that has an id. In this case content_frame. This id is important as this is how you will identify the frame where to place your fragment. You specify multiple FrameLayout's if you want more that one fragment for the same view.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/coordinator_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

      <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary">

        <TextView
            android:id="@+id/textview_toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:layout_gravity="left"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" />

      </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

      <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

  </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

HomeFragment

HomeFragment

就片段而言,您需要包含MvxFragment属性,该属性需要与您要放置片段的Activity关联的Viewmodel类型.此外,您需要指定FrameLayout的ID在活动布局中找到您想要放置片段的位置.

In terms of fragments you need to include the MvxFragment attribute, which needs the type of Viewmodel associate to the Activity you want to place your fragment in. Additionally, you need to specify the id for the FrameLayout that is found on the activities layout, where you want to place the fragment.

[MvxFragment(typeof(MainContainerViewModel), Resource.Id.content_frame)]
[Register(nameof(HomeFragment))]
public class HomeFragment : BaseFragment<HomeViewModel>
{
}


用法

导航时,您现在可以使用ShowViewModel<HomeViewModel>(),它将导航到主页片段.但是,重要的是,在进行片段导航之前,它将首先启动所需的Activity MainActivity.这样可以与不需要这些容器Viewmodel的其他平台更好地共享导航,即通过约定自动处理它们.

When navigating your can now use ShowViewModel<HomeViewModel>() which will navigate to the home fragment. But, importantly it will first start up the required Activity MainActivity before doing the fragment navigation. This allows for better shared navigation with other platforms which do not require these container Viewmodels, i.e. they get handled automatically via convention.

注释

Notes

您可以指定多个MvxFragment属性.如果要在多个活动"下共享相同的片段,则"Is"很有用.顶部MvxFragment属性将用作默认属性.如果您当前处于其他任何匹配的MvxFragment属性的上下文中,则将在该活动下进行导航.

You can specify multiple MvxFragment attributes. Is is usefully if you want the same fragment shared under multiple Activities. The Top MvxFragment attribute will be used as the default. If you are currently in the context of any of the other matching MvxFragment attributes then navigation will take place under that activity.

如果您的Setup.cs不是继承自MvxAppCompatSetup,或者您使用的是自定义演示者,则需要确保您还针对IMvxAndroidViewPresenter注册了演示者.这很重要,因为MvxCachingFragmentCompatActivityMvxCachingFragmentActivity解析IMvxAndroidViewPresenter以便导航到所需的片段.

If your Setup.cs is not inheriting from MvxAppCompatSetup or you are using a custom presenter, you need to make sure that you also registering your presenter against IMvxAndroidViewPresenter. This is important as MvxCachingFragmentCompatActivity or MvxCachingFragmentActivity resolve IMvxAndroidViewPresenter in order to navigate to the required fragment.

protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
    var mvxFragmentsPresenter = new MvxFragmentsPresenter(AndroidViewAssemblies);
    Mvx.RegisterSingleton<IMvxAndroidViewPresenter>(mvxFragmentsPresenter);
    return mvxFragmentsPresenter;
}

您还可以查看示例存储库作为示例在使用中.

You can also check out the Sample Repo for an example of this in use.

这篇关于如何使用MvxCachingFragmentActivity的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆