0

Necesito hacer un scroll view infinito,pero no sé como. Este es mi codigo:

 string[] array1 = { "hotel", "a", "b", "c", "c", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b" };
         StackLayout stackLayout = new StackLayout
             {Orientation = StackOrientation.Horizontal,
                BackgroundColor = Color.Goldenrod,
                HorizontalOptions = LayoutOptions.CenterAndExpand,};
      Content = new ScrollView{VerticalOptions = LayoutOptions.Start,
                    Orientation = ScrollOrientation.Horizontal,
                    HorizontalOptions = LayoutOptions.CenterAndExpand,Content = stackLayout,};
for (int i = 0; i < array1.Length; i++)
            {foreach (var na in array1) {stackLayout.Children.Add
                           (new Button{
                            Text = na,
                            TextColor = Color.Gray, FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Button)) }
                           ); } }

1 respuesta 1

0

Estimado:

Lo que tu necesitas es un ListView. Imagínate un StackLayout que "consume" items desde una colección/enumerable, y los apila verticalmente de acuerdo a una plantilla por cada item de la colección.

Es mucho más fácil usar XAML que hacer las vistas a mano, te sugiero hacer algo como lo siguiente:

  • Crea un proyecto Forms en blanco
  • Abre el MainPage y quita el Label por defecto, reemplazándolo por:

    <ListView ItemsSource="{Binding array1}" BackgroundColor="Goldenrod">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Button Text="{Binding}" TextColor="Gray" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
    

  • Reemplaza el contenido de la clase MainPage que está en el code-behind (lo que aparece en el archivo MainPage.xaml.cs) por esto:

    public string[] array1 { get; set; }
    
    public MainPage()
    {
        InitializeComponent();
        array1 = new string[]{ "hotel", "a", "b", "c", "c", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b" };
        BindingContext = this;
    }
    

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.