首页 > 解决方案 > Xamarin.forms - 滚动时“附加”StackLayout

问题描述

我正在尝试从引导程序中“附加”效果。当用户滚动页面时,我想StackLayout停在他父母的顶部。

有什么办法吗?

这就是我要问的(我在这个例子中使用视差效果):

[1]

谢谢。

标签: xamarinscrollxamarin.formsstacklayoutbootstrap-affix

解决方案


我的评论“为什么不使用具有内置此功能的分组的 ListView?” 部分正确。

使用带有分组的 ListView 将自动在 iOS 上提供“Sticky Header”功能,但在 Android 上不提供。

因此,对于 iOS,下一个代码将起作用:

public class ToysList : List<Toy>
{
    public string Header { get; set; }
    public List<Toy> Toys => this;
}

public class Toy
{
    public string Name { get; set; }
}

// Initialise Toys in your ViewModel

<ListView
    ItemsSource="{Binding Toys}"
    IsGroupingEnabled="true">
    <ListView.GroupHeaderTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding Header}" />
            </ViewCell>
        </DataTemplate>
    </ListView.GroupHeaderTemplate>

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
               <Label Text="{Binding Name}" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

对于 Android,您必须创建一个自定义渲染器,幸运的是github 上有一个很好的示例


推荐阅读