MVVMCross 4.1.4中的片段不会显示在活动(Android)

时间:2016-05-25 09:58:26

标签: android-fragments xamarin mvvmcross

我尝试将一个片段添加到一个活动中,并且已经查看了Martijn00中的example

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

片段:

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:

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

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(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>

1 个答案:

答案 0 :(得分:1)

您的活动必须属于MvxCachingFragmentCompatActivityMvxCachingFragmentActivity类型才能支持托管片段。

其次你需要忽略正常的充气机,然后使用绑定膨胀:

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