コード例 #1
0
ファイル: MainActivity.cs プロジェクト: mrigger/benchmarks
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            var metrics = new DisplayMetrics();

            WindowManager.DefaultDisplay.GetMetrics(metrics);

            float spacing = 40;

            var root = new LayoutItem(this)
            {
                Width         = metrics.WidthPixels,
                Height        = metrics.HeightPixels,
                PaddingBottom = 80
            };

            var label = new Item <TextView>(this)
            {
                Margin = spacing
            };

            label.View.Text  = "This is the list of items you have added. You can add new items and clear the list using the buttons at the bottom. This label has extra lines on purpose.";
            label.SelfSizing = delegate(Xamarin.Flex.Item item, ref float width, ref float height)
            {
                height = new StaticLayout(label.View.Text, label.View.Paint, (int)width, Layout.Alignment.AlignNormal, 1, 0, true).Height;
            };
            root.Add(label);

            var list = new Item <ListView>(this)
            {
                Grow        = 1,
                MarginLeft  = spacing,
                MarginRight = spacing
            };

            list.View.Adapter = new ArrayAdapter(this, Resource.Layout.TextViewItem);
            root.Add(list);

            var input = new Item <EditText>(this)
            {
                Margin = spacing
            };

            input.View.Hint = "Enter list item";
            input.Height    = 80;
            root.Add(input);

            var buttons_row = new LayoutItem(this)
            {
                Direction    = Xamarin.Flex.Direction.Row,
                Height       = 80,
                MarginLeft   = spacing,
                MarginRight  = spacing,
                MarginBottom = spacing
            };

            root.Add(buttons_row);

            var add_button = new Item <Button>(this)
            {
                Grow   = 1,
                Height = 80
            };

            add_button.View.Text   = "Add";
            add_button.View.Click += delegate
            {
                var adapter = (ArrayAdapter)list.View.Adapter;
                if (input.View.Text != "")
                {
                    adapter.Add(input.View.Text);
                    input.View.Text = "";
                }
            };
            buttons_row.Add(add_button);

            var clear_button = new Item <Button>(this)
            {
                Grow   = 1,
                Height = 80
            };

            clear_button.View.Text   = "Clear";
            clear_button.View.Click += delegate
            {
                var adapter = (ArrayAdapter)list.View.Adapter;
                adapter.Clear();
            };
            buttons_row.Add(clear_button);

            root.Layout();

            SetContentView(root.view);
        }
コード例 #2
0
        public static void GeneralLayoutTests() => Describe(nameof(LayoutItemNode), () =>
        {
            It("When adding a child, parent is set", () =>
            {
                var parentNode = new LayoutItem();
                var childNode  = new LayoutItem();
                parentNode.Add(childNode);

                childNode.ShouldSatisfyAllConditions(
                    () => childNode.Parent.ShouldNotBeNull(),
                    () => childNode.Parent.ShouldBe(parentNode)
                    );
            });

            It("When adding a child, it is in collection", () =>
            {
                var parentNode = new LayoutItem();
                var childNode  = new LayoutItem();
                parentNode.Add(childNode);

                parentNode.ShouldContain(childNode);
            });

            It("When setting the parent, it is in collection", () =>
            {
                var parentNode   = new LayoutItem();
                var childNode    = new LayoutItem();
                childNode.Parent = parentNode;

                parentNode.ShouldContain(childNode);
            });

            It("When unsetting the parent, it is removed from collection", () =>
            {
                var parentNode   = new LayoutItem();
                var childNode    = new LayoutItem();
                childNode.Parent = parentNode;
                childNode.Parent = null;

                parentNode.ShouldNotContain(childNode);
            });

            It("When removing a child, it is removed from collection", () =>
            {
                var parentNode = new LayoutItem();
                var childNode  = new LayoutItem();
                parentNode.Add(childNode);
                parentNode.Remove(childNode);

                parentNode.ShouldNotContain(childNode);
            });

            It("When removing a child, it's parent is unset", () =>
            {
                var parentNode = new LayoutItem();
                var childNode  = new LayoutItem();
                parentNode.Add(childNode);
                parentNode.Remove(childNode);

                childNode.Parent.ShouldBeNull();
            });

            It("When setting a new parent, tree structure is correct", () =>
            {
                var rootNode   = new LayoutItem();
                var childNode1 = new LayoutItem();
                var childNode2 = new LayoutItem();
                rootNode.Add(childNode1);
                rootNode.Add(childNode2);
                childNode2.Parent = childNode1;

                rootNode.ShouldSatisfyAllConditions(
                    () => rootNode.Parent.ShouldBeNull(),
                    () => childNode1.Parent.ShouldBe(rootNode),
                    () => childNode2.Parent.ShouldBe(childNode1),
                    () => rootNode.ShouldContain(childNode1),
                    () => childNode1.ShouldContain(childNode2),
                    () => childNode2.ShouldBeEmpty()
                    );
            });

            It("settings parent twice has no effect", () =>
            {
                var rootNode  = new LayoutItem();
                var childNode = new LayoutItem();
                rootNode.Add(childNode);

                childNode.ShouldSatisfyAllConditions(
                    () => childNode.Parent.ShouldBe(rootNode)
                    );
            });

            It("clear unsets the parents", () =>
            {
                var rootNode   = new LayoutItem();
                var childNode1 = new LayoutItem();
                var childNode2 = new LayoutItem();
                var childNode3 = new LayoutItem();
                var childNode4 = new LayoutItem();
                rootNode.Add(childNode1);
                rootNode.Add(childNode2);
                rootNode.Add(childNode3);
                rootNode.Add(childNode4);

                rootNode.Clear();

                rootNode.ShouldSatisfyAllConditions(
                    () => rootNode.ShouldBeEmpty(),
                    () => childNode1.Parent.ShouldBeNull(),
                    () => childNode2.Parent.ShouldBeNull(),
                    () => childNode3.Parent.ShouldBeNull(),
                    () => childNode4.Parent.ShouldBeNull()
                    );
            });

            It("when using with expression sets parent", () =>
            {
                var rootNode   = new LayoutItem();
                var childNode1 = new LayoutItem();
                var childNode2 = new LayoutItem();

                rootNode = rootNode with
                {
                    Children = new()
                    {
                        childNode1,
                        childNode2,
                        new LayoutItem()
                    }
                };