简介
我是C#编程的新手,刚刚开始学习Xamarin Forms。我在Windows上的Visual Studio 2017中使用Xamarin来制作跨平台应用程序。
现状
我创建了一个名为Buy的TabbedPage,它有一个名为BuyAnimals的内容页。
ContentPage需要有一个ScrollView,它有一个StackLayout作为其子代。
StackLayout有3个子帧,即Frames。
在每个帧中,应该是一个名为animalStack的StackLayout。
animalStack有许多来自StackLayout的孩子。
问题。
如果我使用相同的animalStack作为三个帧中每个帧的内容,则只有最后一帧显示animalStack的内容,而其他两帧不显示任何内容。
为什么其他帧没有显示最后一帧中显示的信息的副本?如何让他们显示信息?
我的代码如下所示。
`public partial class Buy:TabbedPage { 公众买() { 的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;
}
}`
我真的很感激你的意见。
感谢。
答案 0 :(得分: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。
我希望它可以帮到你。