Как использовать вертикальную прокрутку для отображения данных привязки - PullRequest
0 голосов
/ 31 октября 2018

Я хочу такую ​​структуру: Нажмите, чтобы увидеть желаемый результат

Но с моим кодом я получаю это: Нажмите это, чтобы увидеть вывод

Вот мой код xaml:

<ScrollView Orientation="Horizontal">
   <StackLayout Orientation="Horizontal" VerticalOptions="Start">
     <Grid  x:Name="ImagesListViews" >
         <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
         </Grid.RowDefinitions>
         <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*" />
         </Grid.ColumnDefinitions>
    </Grid>

    <local:BindableStackLayout x:Name="featuredEventsList">
       <local:BindableStackLayout.ItemDataTemplate>
         <DataTemplate>

          <StackLayout Orientation="Vertical" Padding="0" Margin="-5,0,5,0" HorizontalOptions="Center" >
            <StackLayout.GestureRecognizers>
                <TapGestureRecognizer  NumberOfTapsRequired="1" />
            </StackLayout.GestureRecognizers>
            <Image Source="{Binding ImageThumbURL}" Margin="0,0,0,0" WidthRequest="140" />
            <Label Margin="0" Text="{Binding TitleInPrimaryLang}" FontSize="12" TextColor="Black" LineBreakMode="TailTruncation" WidthRequest="100"/>

         </StackLayout>
       </DataTemplate>
     </local:BindableStackLayout.ItemDataTemplate>
   </local:BindableStackLayout>
 </StackLayout>
</ScrollView>

Любая помощь будет принята с благодарностью. Спасибо

1 Ответ

0 голосов
/ 01 ноября 2018

Вы должны настроить управление для этого. Пожалуйста, пройдите через это и дайте мне знать, если какой-либо запрос.

1) Расширьте представление прокрутки с помощью пользовательского шаблона.

public class HorizontalListview : ScrollView
{
    public static readonly BindableProperty ItemsSourceProperty =
        BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(HorizontalListview), default(IEnumerable));

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly BindableProperty ItemTemplateProperty =
        BindableProperty.Create("ItemTemplate", typeof(DataTemplate), typeof(HorizontalListview), default(DataTemplate));

    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

    public event EventHandler<ItemTappedEventArgs> ItemSelected;

    public static readonly BindableProperty SelectedCommandProperty =
        BindableProperty.Create("SelectedCommand", typeof(ICommand), typeof(HorizontalListview), null);

    public ICommand SelectedCommand
    {
        get { return (ICommand)GetValue(SelectedCommandProperty); }
        set { SetValue(SelectedCommandProperty, value); }
    }

    public static readonly BindableProperty SelectedCommandParameterProperty =
        BindableProperty.Create("SelectedCommandParameter", typeof(object), typeof(HorizontalListview), null);

    public object SelectedCommandParameter
    {
        get { return GetValue(SelectedCommandParameterProperty); }
        set { SetValue(SelectedCommandParameterProperty, value); }
    }


    public void Render()
    {
        if (ItemTemplate == null || ItemsSource == null)
            return;

        var layout = new StackLayout();
        layout.Spacing = 0;

        layout.Orientation = Orientation == ScrollOrientation.Vertical ? StackOrientation.Vertical : StackOrientation.Horizontal;

        foreach (var item in ItemsSource)
        {
            var command = SelectedCommand ?? new Command((obj) =>
            {
                var args = new ItemTappedEventArgs(ItemsSource, item);
                ItemSelected?.Invoke(this, args);
            });
            var commandParameter = SelectedCommandParameter ?? item;

            var viewCell = ItemTemplate.CreateContent() as ViewCell;
            viewCell.View.BindingContext = item;
            viewCell.View.GestureRecognizers.Add(new TapGestureRecognizer
            {
                Command = command,
                CommandParameter = commandParameter,
                NumberOfTapsRequired = 1
            });
            layout.Children.Add(viewCell.View);
        }

        Content = layout;
    }
}

2) Добавьте верхнюю часть пространства имен на свою страницу.

xmlns:control="clr-namespace:**Projectname**.CustomControls"

3) Использовать контроль,

 <control:HorizontalListview  Orientation="Horizontal">
            <control:HorizontalListview.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <!....Your Design.....>
                    </ViewCell>
                </DataTemplate>
            </control:HorizontalListview.ItemTemplate>
 </control:HorizontalListview>

4) Свяжите свои данные.

**YourControlName**.ItemsSource = lLstPhotoGallery; // Your List
**YourControlName**.Render();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...