MVVMCross 4.1.4中的片段将不会显示在“活动"中(Android) [英] Fragments in MVVMCross 4.1.4 will not be displayed in the Activity (Android)

查看:60
本文介绍了MVVMCross 4.1.4中的片段将不会显示在“活动"中(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将片段添加到活动中,并且已经查看了示例来自Martijn00.

I try to add a fragment to an activity and have already looked at the example from Martijn00.

我的问题是,将显示活动,但片段不会显示,并且在调试模式下,调试器未运行到片段的OnCreateView方法中.因此,将使用片段的属性,但不使用片段.我只看到绿色背景的活动.我确定我做错了什么,但是6个小时后,我确实放弃了,请大家帮忙:)

My problem is, that the activity will be displayed but the fragment will not and in debugging mode, the debugger is not running into the fragment's method OnCreateView. So the attribute of the fragment will be used, but not the fragment. I see only my activity with the green background. I am sure I do something wrong, but after 6 hours I do give up and ask you guys for help :)

片段:

namespace TopDownTodoList.Mobile.Droid.Views.Fragments{

[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true)]
[Register("topdowntodolist.mobile.droid.views.fragments.TodoOverviewFragment")]
public class TodoOverviewFragment :  MvxFragment<TodoOverviewViewModel>
{
    protected int LayoutId => Resource.Layout.f_todo_overview_fragment;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        this.Init();
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return this.BindingInflate(this.LayoutId, null);
        //return inflater.Inflate(this.LayoutId, container, false);
    }

    public virtual void Init() { }
}}

活动:

namespace TopDownTodoList.Mobile.Droid.Views{

[Activity(Label = "MainView")]
public class MainView : MvvmCross.Droid.FullFragging.Views.MvxActivity<MainViewModel>
{
    protected int LayoutId => Resource.Layout.main_view;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(this.LayoutId);

        this.Init();
    }

    public virtual void Init(){}
}}

App.cs:

        protected override void RegisterClasses()
    {
        RegisterAppStart<TodoOverviewViewModel>();
    }

ViewModels:

ViewModels:

public class VMName : MvvmCross.Core.ViewModels.MvxViewModel{...}

FragmentLayout(f_todo_overview_fragment):

FragmentLayout (f_todo_overview_fragment):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mvx="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#0000aa"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:gravity="center"/></LinearLayout>

ActivityLayout(主视图):

ActivityLayout (main_view):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#aa0000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"><FrameLayout
  android:id="@+id/content_frame"
    android:background="#00bb00"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true" /></LinearLayout>

推荐答案

您的活动必须为MvxCachingFragmentCompatActivityMvxCachingFragmentActivity类型,以支持片段的托管.

Your Activity needs to be of the type MvxCachingFragmentCompatActivity or MvxCachingFragmentActivity to support hosting of fragments.

其次,您需要忽略普通的充气机,然后使用绑定充气:

Secondly you need to ignore the normal inflator, and use binding inflate after that:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var ignore = base.OnCreateView(inflater, container, savedInstanceState);
    return this.BindingInflate(FragmentId, null);
}

这篇关于MVVMCross 4.1.4中的片段将不会显示在“活动"中(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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