【问题标题】:xamarin forms add duplicate stacklayout object to a scrollViewxamarin 表单将重复的 stacklayout 对象添加到 scrollView
【发布时间】:2017-12-30 10:25:41
【问题描述】:

简介

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

现状

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

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

StackLayout 有 3 个子框架。

在每个框架内,应该有一个名为 animalStack 的 StackLayout。

animalStack 有许多由 StackLayout 制成的子代。

问题。

如果我使用相同的 animalStack 作为三个 Frame 中的每一个的内容,则只有最后一个 frame 显示 animalStack 的内容,而其他两个 frame 什么也不显示。

为什么其他帧没有显示上一帧中显示的信息的副本?我怎样才能让他们显示信息?

我的代码如下所示。

`public partial class Buy : TabbedPage { 公开购买 () { 初始化组件();

        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;


    }
}`

非常感谢您的意见。

谢谢。

【问题讨论】:

  • 您不能多次将同一个视图实例添加到布局中。您需要创建一个新实例。
  • 似乎 StackLayout 对象的某些属性(以及其他类似属性)是在基于其父级显示时定义/计算的,因此同一实例不能有多个父级。
  • 你为什么要重用同一个实例?
  • 虽然,在这种特殊情况下,我猜您可能会使用 ListView 并将您的布局定义为 ItemTemplate ViewCell。我会用一些代码来回答你是否合适(我真的建议你也使用绑定)

标签: c# xamarin.ios xamarin.android xamarin.forms scrollview


【解决方案1】:

看来这就是你想做的:

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 了解绑定和this one 了解列表视图模板。

希望对你有帮助。

【讨论】:

  • 谢谢。这行得通。必须在创建 listView 对象后添加listView.HasUnevenRows = true 以显示剩余的标签。队友的欢呼声。你在坦桑尼亚救了一个伙伴。
  • 我很乐意提供帮助。抱歉,忘记了“HasUnevenRows”,这通常是必要的,真的。如果您需要帮助,可以指望我 =)
猜你喜欢
  • 1970-01-01
  • 2019-08-18
  • 2020-03-21
  • 2016-08-13
  • 1970-01-01
  • 1970-01-01
  • 2015-12-04
  • 2018-01-19
  • 1970-01-01
相关资源
最近更新 更多