0
votes

I need to dynamically create MvxFrameControll, inflate it with content and add to another Layout (FrameLayout / LinearLayout). My code is

HomeScreen.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<krista.fm.iMonitoring.Android.Native.TopBar
    android:layout_width="match_parent"
    android:layout_height="50dp"
    local:MvxBind="DataContext TopBar"
    local:MvxTemplate="@layout/topbar"
    android:id="@+id/TopBar" />
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/SubjectBodyContainer" />
 </LinearLayout>


public class SubjectBody : MvxFrameControl
{
    public SubjectBody (Context context, IAttributeSet attrs) : base (context, attrs)
    {
    }

    public SubjectBody (int templateId, Context context, IAttributeSet attrs) : base (context, attrs)
    {            
    }

    protected override void OnContentSet ()
    {
        base.OnContentSet ();
    }
}

HomeScreenView

[Activity (Label = "HomeScreen")]
public class HomeScreenView : MvxActivity
{
    public override void OnAttachedToWindow()
    {
        base.OnAttachedToWindow();
    }

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.HomeScreen);

        AddSubjectBody();
    }

    protected override void OnStart()
    {
        base.OnStart();
    }

    protected virtual void AddSubjectBody()
    {
        var container = FindViewById<FrameLayout>(Resource.Id.SubjectBodyContainer);
        var subjectBody = new SubjectBody(Resource.Layout.SubjectBody, this, null);
        subjectBody.DataContext = new SubjectBodyViewModel();

        container.AddView(subjectBody, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
    }
}

Why this code doesn't add content on screen and how it rewrite rightly? When I use SubjectBody adding inside axml with templateId it works correctly. But what if i need adding this dynamically?

protected virtual void AddSubjectBody()
{
    var container = FindViewById<FrameLayout>(Resource.Id.SubjectBodyContainer);
    var subjectBody = new SubjectBody(Resource.Layout.SubjectBody, this, null);
    subjectBody.DataContext = new SubjectBodyViewModel();

    container.AddView(subjectBody, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
}
1

1 Answers

0
votes

Are you sure your code is running on the UI thread? I'm using pretty a similar code in one app and the view gets added into the parent. What I suggest you to try is:

this.RunOnUiThread(() => container.AddView(this.subjectBody));