Xamarin.forms 水平列表视图Xamarin表单

Xamarin.forms 水平列表视图Xamarin表单,xamarin.forms,xamarin.forms.listview,Xamarin.forms,Xamarin.forms.listview,我试图在Xamarin表单中实现水平滚动列表视图,我尝试了几个库,但没有找到好的解决方案。这在Xamrin表单(不带渲染)中可能吗?是否有我可以使用的工作库?试试看 示例用法:(XAML) <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="test.ListPage" xmlns:Control

我试图在Xamarin表单中实现水平滚动列表视图,我尝试了几个库,但没有找到好的解决方案。这在Xamrin表单(不带渲染)中可能吗?是否有我可以使用的工作库?

试试看

示例用法:(XAML)

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
x:Class="test.ListPage" xmlns:Controls="clr-namespace:test;assembly=test"> 

<Controls:HorizontalListView ItemsSource="{Binding Categories}" ListOrientation="Horizontal"> 
  <Controls:HorizontalListView.ItemTemplate> 
    <DataTemplate> 
    <Label Text="{Binding Name}" Grid.Row="0" YAlign="Center" /> 
    </DataTemplate> 
  </Controls:HorizontalListView.ItemTemplate> 
  </Controls:myControl>

为了获得水平滚动列表视图,您必须创建一个自定义滚动视图,如下所示,并在XAML中使用此自定义控件

公共类图像库:滚动视图 { 只读堆栈布局_imageStack

    public ImageGallery()
    {
        this.Orientation = ScrollOrientation.Horizontal;

        _imageStack = new StackLayout
        {
            Orientation = StackOrientation.Horizontal
        };

        this.Content = _imageStack;
    }

    public IList<View> Children
    {
        get
        {
            return _imageStack.Children;
        }
    }


    public static readonly BindableProperty ItemsSourceProperty =
        BindableProperty.Create<ImageGallery, IList>(
            view => view.ItemsSource,
            default(IList),
            BindingMode.TwoWay,
            propertyChanging: (bindableObject, oldValue, newValue) => {
                ((ImageGallery)bindableObject).ItemsSourceChanging();
            },
            propertyChanged: (bindableObject, oldValue, newValue) => {
                ((ImageGallery)bindableObject).ItemsSourceChanged(bindableObject, oldValue, newValue);
            }
        );

    public IList ItemsSource
    {
        get
        {
            return (IList)GetValue(ItemsSourceProperty);
        }
        set
        {

            SetValue(ItemsSourceProperty, value);
        }
    }
    void ItemsSourceChanging()
    {
        if (ItemsSource == null)
            return;
    }
    void ItemsSourceChanged(BindableObject bindable, IList oldValue, IList newValue)
    {
        if (ItemsSource == null)
            return;

        var notifyCollection = newValue as INotifyCollectionChanged;
        if (notifyCollection != null)
        {
            notifyCollection.CollectionChanged += (sender, args) => {
                if (args.NewItems != null)
                {
                    foreach (var newItem in args.NewItems)
                    {

                        var view = (View)ItemTemplate.CreateContent();
                        var bindableObject = view as BindableObject;
                        if (bindableObject != null)
                            bindableObject.BindingContext = newItem;
                        _imageStack.Children.Add(view);
                    }
                }
                if (args.OldItems != null)
                {
                    // not supported
                    _imageStack.Children.RemoveAt(args.OldStartingIndex);
                }
            };
        }
    }
    public DataTemplate ItemTemplate
    {
        get;
        set;
    }
    public static readonly BindableProperty SelectedItemProperty =
        BindableProperty.Create<ImageGallery, object>(
            view => view.SelectedItem,
            null,
            BindingMode.TwoWay,
            propertyChanged: (bindable, oldValue, newValue) => {
                ((ImageGallery)bindable).UpdateSelectedIndex();
            }
        );

    public object SelectedItem
    {
        get
        {
            return GetValue(SelectedItemProperty);
        }
        set
        {
            SetValue(SelectedItemProperty, value);
        }
    }

    void UpdateSelectedIndex()
    {
        if (SelectedItem == BindingContext)
            return;

        SelectedIndex = Children
            .Select(c => c.BindingContext)
            .ToList()
            .IndexOf(SelectedItem);

    }

    public static readonly BindableProperty SelectedIndexProperty =
        BindableProperty.Create<ImageGallery, int>(
            carousel => carousel.SelectedIndex,
            0,
            BindingMode.TwoWay,
            propertyChanged: (bindable, oldValue, newValue) => {
                ((ImageGallery)bindable).UpdateSelectedItem();
            }
        );

    public int SelectedIndex
    {
        get
        {
            return (int)GetValue(SelectedIndexProperty);
        }
        set
        {
            SetValue(SelectedIndexProperty, value);
        }
    }

    void UpdateSelectedItem()
    {
        SelectedItem = SelectedIndex > -1 ? Children[SelectedIndex].BindingContext : null;
    }
}`
公共图像库()
{
this.Orientation=ScrollOrientation.Horizontal;
_imageStack=新StackLayout
{
方向=堆叠方向。水平
};
this.Content=\u imageStack;
}
公营儿童
{
得到
{
返回_imageStack.Children;
}
}
公共静态只读BindableProperty项SourceProperty=
BindableProperty.Create(
view=>view.ItemsSource,
违约(IList),
BindingMode.TwoWay,
属性更改:(bindableObject、oldValue、newValue)=>{
((ImageGallery)bindableObject.ItemsSourceChange();
},
propertyChanged:(bindableObject、oldValue、newValue)=>{
((ImageGallery)bindableObject).ItemsSourceChanged(bindableObject,oldValue,newValue);
}
);
公共IList项目资源
{
得到
{
返回(IList)GetValue(ItemsSourceProperty);
}
设置
{
设置值(ItemsSourceProperty,value);
}
}
作废项目采购更改()
{
if(ItemsSource==null)
返回;
}
void ItemsSourceChanged(BindableObject bindable、IList oldValue、IList newValue)
{
if(ItemsSource==null)
返回;
var notifyCollection=INOTIFYCOLLECTION更改时的新值;
if(notifyCollection!=null)
{
notifyCollection.CollectionChanged+=(发件人,参数)=>{
如果(args.NewItems!=null)
{
foreach(args.NewItems中的var newItem)
{
var view=(view)ItemTemplate.CreateContent();
var bindableObject=作为bindableObject查看;
if(bindableObject!=null)
bindableObject.BindingContext=newItem;
_imageStack.Children.Add(视图);
}
}
如果(args.OldItems!=null)
{
//不支持
_imageStack.Children.RemoveAt(args.OldStartingIndex);
}
};
}
}
公共数据模板ItemTemplate
{
得到;
设置
}
公共静态只读BindableProperty SelectedItemProperty=
BindableProperty.Create(
view=>view.SelectedItem,
无效的
BindingMode.TwoWay,
属性更改:(可绑定、旧值、新值)=>{
((ImageGallery)可绑定).UpdateSelectedIndex();
}
);
公共对象SelectedItem
{
得到
{
返回GetValue(SelectedItemProperty);
}
设置
{
设置值(SelectedItemProperty,value);
}
}
void UpdateSelectedIndex()
{
if(SelectedItem==BindingContext)
返回;
SelectedIndex=子级
.Select(c=>c.BindingContext)
托利斯先生()
.IndexOf(SelectedItem);
}
公共静态只读BindableProperty SelectedIndexProperty=
BindableProperty.Create(
carousel=>carousel.SelectedIndex,
0,
BindingMode.TwoWay,
属性更改:(可绑定、旧值、新值)=>{
((ImageGallery)可绑定);
}
);
公共整数选择索引
{
得到
{
返回(int)GetValue(SelectedIndexProperty);
}
设置
{
SetValue(SelectedIndexProperty,value);
}
}
void UpdateSelectedItem()
{
SelectedItem=SelectedIndex>-1?子项[SelectedIndex]。BindingContext:null;
}
}`
现在,您可以使用XAML中的自定义滚动视图来获取水平滚动列表



有些人通过旋转ListView,然后再旋转回元素来创建水平ListView


这是一个黑客,你必须确保它不会弄乱你的布局(例如,不要把它放在网格中)。但它很好用。

也许是旋转木马视图?谢谢。我试试这个。谢谢。我试试这个。