2017-07-24 41 views
0

介绍xamarin形式添加重复stacklayout对象滚动型

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

现状

我创建了一个名为购买TabbedPage,它有一个叫contentPage BuyAnimals。

ContentPage需要有一个ScrollView,它有一个单独的StackLayout作为其子。

StackLayout有3个子框架。

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

animalStack有很多由StackLayout制作的孩子。

问题。

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

为什么其他框架不显示最后一帧中显示的信息的重复?我如何让他们显示信息?

我的代码如下所示。

`公共部分类购买: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

您无法多次将相同的视图实例添加到布局。您需要改为创建一个新实例。 – Jason

+0

看起来像StackLayout对象(和其他类似对象)的某些属性是基于其父项显示时定义/计算的,因此它的实例的相同项不能有多个父项。 –

+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关于绑定和this one关于listview模板。

我希望它能帮助你。

+0

Thanks.This工作。必须在创建listView对象后添加'listView.HasUnevenRows = true'来显示剩余的标签。队友的欢呼声。你在坦桑尼亚拯救了一个伴侣。 –

+0

我很乐意提供帮助。对不起忘了'HasUnevenRows',这真的很有必要。如果您需要帮助,您可以指望我)=) –