首页 文章

xamarin表单将重复的stacklayout对象添加到scrollView

提问于
浏览
0

Introduction

我是C#编程的新手,刚刚开始学习Xamarin Forms . 我在Windows上的Visual Studio 2017中使用Xamarin来制作跨平台应用程序 .

Current Situation

我创建了一个名为Buy的TabbedPage,它有一个名为BuyAnimals的contentPage .

ContentPage需要有一个ScrollView,它有一个StackLayout作为其子级 .

StackLayout有3个子帧,它们是Frames .

在每个帧内,应该是一个名为animalStack的StackLayout .

animalStack有许多来自StackLayout的孩子 .

The Problem.

如果我使用相同的animalStack作为三个帧中每个帧的内容,则只有最后一帧显示animalStack的内容,而其他两帧不显示任何内容 .

为什么其他帧没有显示最后一帧中显示的信息的副本?如何让他们显示信息?

My code is as show below.

`public partial class购买:TabbedPage {public Buy(){InitializeComponent();

var animalItemIdStack = new StackLayout
        {
            Children = {
                new Label { Text = "Post ID: "},
                new Label { Text = "Animal ID"}
            },
            Orientation = StackOrientation.Horizontal
        };

        var animalUserIdStack = new StackLayout
        {
            Children = {
                new Label { Text = "user ID: "},
                new Label { Text = "Posting userID"}
            },
            Orientation = StackOrientation.Horizontal
        };

        var animalBreedStack = new StackLayout
        {
            Children = {
                new Label { Text = "Breed: "},
                new Label { Text = "Animal's breed"}
            },
            Orientation = StackOrientation.Horizontal
        };

        var animalGenderStack = new StackLayout
        {
            Children = {
                new Label { Text = "Gender: "},
                new Label { Text = "Animal's gender"}
            },
            Orientation = StackOrientation.Horizontal
        };
        var animalAgeStack = new StackLayout
        {
            Children = {
                new Label { Text = "Age: "},
                new Label { Text = "Animal's age"}
            },
            Orientation = StackOrientation.Horizontal
        };
        var animalColorStack = new StackLayout
        {
            Children = {
                new Label { Text = "Color: "},
                new Label { Text = "Animal's color"}
            },
            Orientation = StackOrientation.Horizontal
        };
        var animalPriceStack = new StackLayout
        {
            Children = {
                new Label { Text = "Price: "},
                new Label { Text = "Animal's price"}
            },
            Orientation = StackOrientation.Horizontal
        };
        var animalLocationStack = new StackLayout
        {
            Children = {
                new Label { Text = "Location: "},
                new Label { Text = "Animal owner's location"}
            },
            Orientation = StackOrientation.Horizontal
        };
        var animalEmailStack = new StackLayout
        {
            Children = {
                new Label { Text = "Location: "},
                new Label { Text = "Animal's location"}
            },
            Orientation = StackOrientation.Horizontal
        };
        var animalPhoneStack = new StackLayout
        {
            Children = {
                new Label { Text = "Phone: "},
                new Label { Text = "Animal owner's phone"}
            },
            Orientation = StackOrientation.Horizontal
        };
        var animalDatePostedStack = new StackLayout
        {
            Children = {
                new Label { Text = "Posted: "},
                new Label { Text = "Posted date"}
            },
            Orientation = StackOrientation.Horizontal
        };
        var animalLastEditStack = new StackLayout
        {
            Children = {
                new Label { Text = "Last Edit: "},
                new Label { Text = "Last Edited date"}
            },
            Orientation = StackOrientation.Horizontal
        };

        var animalStack = new StackLayout
        {
            Children =
            {
                animalItemIdStack,
                animalUserIdStack,
                animalBreedStack,
                animalGenderStack,
                animalAgeStack,
                animalColorStack,
                animalPriceStack,
                animalLocationStack,
                animalEmailStack,
                animalEmailStack,
                animalPhoneStack,
                animalDatePostedStack,
                animalLastEditStack
            },
            Orientation = StackOrientation.Vertical
        };

        var animalFrame = new Frame();
        animalFrame.Content = animalStack;

        var animalFrame2 = new Frame();
        animalFrame2.Content = animalStack;

        var animalFrame3 = new Frame();
        animalFrame3.Content = animalStack;

        var animalFullStack = new StackLayout();

        animalFullStack.Children.Add(animalFrame);
        animalFullStack.Children.Add(animalFrame2);
        animalFullStack.Children.Add(animalFrame3);

        var animalScrollView = new ScrollView();
        animalScrollView.Content = animalFullStack;

        BuyAnimals.Content = animalScrollView;


    }
}`

我非常感谢你的意见 .

谢谢 .

1 回答

  • 0

    似乎这就是你想做的事情:

    public partial class Buy (...){
        InitalizeComponent();
        Content = GetContent();
    }
    
    public View GetContent()
    {
        var animalItemIdStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Post ID: "},
                    new Label { Text = "Animal ID"}
                },
            Orientation = StackOrientation.Horizontal
        };
    
        var animalUserIdStack = new StackLayout
        {
            Children = {
                    new Label { Text = "user ID: "},
                    new Label { Text = "Posting userID"}
                },
            Orientation = StackOrientation.Horizontal
        };
    
        var animalBreedStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Breed: "},
                    new Label { Text = "Animal's breed"}
                },
            Orientation = StackOrientation.Horizontal
        };
    
        var animalGenderStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Gender: "},
                    new Label { Text = "Animal's gender"}
                },
            Orientation = StackOrientation.Horizontal
        };
        var animalAgeStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Age: "},
                    new Label { Text = "Animal's age"}
                },
            Orientation = StackOrientation.Horizontal
        };
        var animalColorStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Color: "},
                    new Label { Text = "Animal's color"}
                },
            Orientation = StackOrientation.Horizontal
        };
        var animalPriceStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Price: "},
                    new Label { Text = "Animal's price"}
                },
            Orientation = StackOrientation.Horizontal
        };
        var animalLocationStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Location: "},
                    new Label { Text = "Animal owner's location"}
                },
            Orientation = StackOrientation.Horizontal
        };
        var animalEmailStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Location: "},
                    new Label { Text = "Animal's location"}
                },
            Orientation = StackOrientation.Horizontal
        };
        var animalPhoneStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Phone: "},
                    new Label { Text = "Animal owner's phone"}
                },
            Orientation = StackOrientation.Horizontal
        };
        var animalDatePostedStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Posted: "},
                    new Label { Text = "Posted date"}
                },
            Orientation = StackOrientation.Horizontal
        };
        var animalLastEditStack = new StackLayout
        {
            Children = {
                    new Label { Text = "Last Edit: "},
                    new Label { Text = "Last Edited date"}
                },
            Orientation = StackOrientation.Horizontal
        };
    
        var animalStack = new StackLayout
        {
            Children =
                {
                    animalItemIdStack,
                    animalUserIdStack,
                    animalBreedStack,
                    animalGenderStack,
                    animalAgeStack,
                    animalColorStack,
                    animalPriceStack,
                    animalLocationStack,
                    animalEmailStack,
                    animalEmailStack,
                    animalPhoneStack,
                    animalDatePostedStack,
                    animalLastEditStack
                },
            Orientation = StackOrientation.Vertical
        };
    
        var animalFrame = new Frame();
        animalFrame.Content = animalStack;
    
    
        var listView = new ListView();
        listView.ItemTemplate = new DataTemplate(() =>
        {
            return new ViewCell { View = animalFrame };
        });
        listView.ItemsSource = new List<object>()
                {
                    new object(),
                    new object(),
                    new object(),
                };
    
        var contentStack = new StackLayout();
        contentStack.Children.Add(listView);
    
        return contentStack;
    }
    

    您需要使用绑定来补充此方法 . 有关绑定的信息,请参见this article,关于listview模板,请参见this one .

    我希望它对你有所帮助 .

相关问题