xamarin формы: добавление фрагмента в xaml - PullRequest
0 голосов
/ 18 февраля 2020

Я пытаюсь использовать шаблон, представленный в этой статье tute , чтобы добавить собственный фрагмент android в пользовательский интерфейс в проекте Xamarin Forms.

Контейнер макета, используемый для Хост, фрагмент отображается на экране без проблем, но не компоненты фрагмента. Результирующая иерархия представлений, как видно из проекта Android, показывает внедренный фрагмент, но кажется, что на самом деле ничего не отображается.

Некоторый код:


    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Util;
    using Android.Views;
    using Android.Widget;
    using Xamarin.Forms;


    namespace NativeFeatures.Droid
    {

        public class TestNativeFragmentViewGroup : ViewGroup
        {
            Context context = Forms.Context;  // Android.App.Application.Context; // this is supposed to be the equivalent replacement but a crash occurs
            Android.Widget.Button buttonTest;

            public TestNativeFragmentViewGroup() : base(Forms.Context)
            {
                context = Forms.Context;  // ? Android.App.Application.Context; 

                this.Id = 43434;

                int minWidth = 300;
                int minHeight = 800;

                SetMinimumHeight(minHeight);
                SetMinimumWidth(minWidth);

                SetBackgroundColor(Android.Graphics.Color.Firebrick);

                buttonTest = new Android.Widget.Button(context);
                buttonTest.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
                buttonTest.SetBackgroundColor(new Android.Graphics.Color(Android.Graphics.Color.ForestGreen));
                buttonTest.BringToFront();
                buttonTest.Text = "Button Test";
                buttonTest.Id = 12347;
                AddView(buttonTest); // this works

                addTestFragment(); // layout appears to be there but no fragment

            }

        LinearLayout ll = null;
            private void addTestFragment()
            {
                ll = new LinearLayout(context);
                ll.Orientation = Orientation.Horizontal;
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManagerLayoutParams.MatchParent, WindowManagerLayoutParams.MatchParent);
                ll.LayoutParameters = layoutParams;
                ll.SetBackgroundColor(Android.Graphics.Color.LightGreen);

                ll.Id = 234567;

                ((Activity)context).FragmentManager.BeginTransaction().Add(ll.Id, TestFragment.newInstance("I am frag 1"), "someTag1").Commit();
                ((Activity)context).FragmentManager.BeginTransaction().Add(ll.Id, TestFragment.newInstance("I am frag 2"), "someTag2").Commit();


                AddView(ll);
            }


            protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
            {

                if (buttonTest != null)
                {
                    buttonTest.Layout(left, top, (right - left) / 5, (bottom - top) / 5);
                }

                if (ll != null)
                {
                    ll.Layout(left, top, right, bottom);
                }


            }

            void UpdateUi()
            {
            }

        }
        public class TestFragment : Fragment
        {

            public static TestFragment newInstance(String text)
            {

                TestFragment f = new TestFragment();

                Bundle b = new Bundle();
                b.PutString("text", text);
                f.Arguments = b;
                return f;
            }

            // @Override
            public override Android.Views.View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                Android.Views.View v = inflater.Inflate(Resource.Layout.layout1, container, false);

                string val = Arguments.GetString("text");
                ((TextView)v.FindViewById(Resource.Id.tvFragText)).SetText(val.ToCharArray(), 0, val.Length);
                return v;
            }
        }


    }

и AndroidView.xaml


    <?xml version="1.0" encoding="UTF-8"?>
    <ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
                xmlns:formsandroid="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"
                xmlns:NativeFeatures="clr-namespace:NativeFeatures.Droid;assembly=NativeFeatures.Android;targetPlatform=Android" 
                 mc:Ignorable="d"
                 x:Class="NativeFeatures.AndroidView">
      <ContentView.Content>
          <StackLayout>
                <Label Text="Hello Xamarin.Forms Should just be Android Content!" />
                <NativeFeatures:TestNativeFragmentViewGroup/>
            </StackLayout>
      </ContentView.Content>

Я что-то упустил? Или xamarin не поддерживает нативные фрагменты таким образом?

...