简体   繁体   English

如何在 Xamarin 表单的分组 ListView 中创建页脚模板

[英]How to create a footer template in a Grouped ListView in Xamarin Form

I'm working with Xamarin Form and C#.我正在使用 Xamarin Form 和 C#。 So, I have a listview in a Xamarin project which I enable the property IsGroupingEnabled.所以,我在 Xamarin 项目中有一个列表视图,我启用了属性 IsGroupingEnabled。 The items and header of items work perfectly but I cannot set a footer template.项目和项目的标题工作正常,但我无法设置页脚模板。 I need to create a listview, items must be composed of:我需要创建一个列表视图,项目必须由以下组成:

  1. Header: User information.标题:用户信息。
  2. Items: List of subitems.项目:子项目列表。
  3. Footer: Actions buttons (shared, comments, etc).页脚:操作按钮(共享、评论等)。

This is part of my code:这是我的代码的一部分:

Model:模型:

public class Post
{
    public long PostID { get; set; }
    public string Name { get; set; }
    public List<OptionDefault> OptionsDefault { get; set; }
}
public class OptionDefault
{
    public long OptionTypeID { get; set; }
    public string SubItemName { get; set; }
}

ViewModel视图模型

public class PostsViewModel
{
    public InfiniteScrollCollection<Grouping<Post, OptionDefault>> Items { get; set; } = new InfiniteScrollCollection<Grouping<Post, OptionDefault>>();
    public IPostsService repository;

    public PostsViewModel(INavigation navigation)
    {
        repository = new PostsService();

        Items = new InfiniteScrollCollection<Grouping<Post, OptionDefault>>
        {
            OnLoadMore = async () => await GetItems()
        };
        Items.LoadMoreAsync();
    }
    async Task<List<Grouping<Post, OptionDefault>>> GetItems()
    {
        IsWorking = true;
        List<Grouping<Post, OptionDefault>> items = new List<Grouping<Post, OptionDefault>>();
        try
        {
            // load the next page
            var lists = await repository.GetList(Items.Count);
            foreach (var item in lists)
            {
                for (int i = 0; i < item.OptionsDefault.Count; i++)
                {
                    if ((i + 1) == item.OptionsDefault.Count)
                        item.OptionsDefault[i].LastItem = true;
                }
                var group = new Grouping<Post, OptionDefault>(item, item.OptionsDefault);
                items.Add(group);
            }
        }
        catch (Exception ex)
        {
            ErrorHelper.RegisterError(ex);
        }
        finally
        {
            IsWorking = false;
        }
        return items;
    }

.xaml: .xaml:

<ListView ItemsSource="{Binding Items}"                     
            IsGroupingEnabled="True">
        <ListView.GroupHeaderTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Margin="5,15,5,0">
                        <Label Text="{Binding Key.User.UserName}" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.GroupHeaderTemplate>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Margin="5,0,5,5">
                        <Label Text="{Binding SubItemName}" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

I've tried using footertemplate, but it doesn't work:我试过使用页脚模板,但它不起作用:

<ListView.FooterTemplate>
            <DataTemplate>
                <StackLayout Margin="5,0,5,5">
                    <Label Text="This is my footer" />
                </StackLayout>
            </DataTemplate>
        </ListView.FooterTemplate>

Whether it's <ListView.Footer> or <ListView.FooterTemplate> , the display position is the same, right at the bottom of the ListView.无论是<ListView.Footer>还是<ListView.FooterTemplate> ,显示位置都是一样的,就在 ListView 的底部。 Currently, ListView of xamarin form does not have this property to make each items have a footer.目前,xamarin 形式的 ListView 没有这个属性来让每个项目都有一个页脚。 Maybe you need to rethink the requirements and UI design of your app.也许您需要重新考虑应用程序的要求和 UI 设计。

CollectionView is now available in Xamarin.Forms. CollectionView 现在在 Xamarin.Forms 中可用。 It have both GroupHeaderTemplate and GroupFooterTemplate .它有GroupHeaderTemplateGroupFooterTemplate You can use it to show the Group of data.您可以使用它来显示数据组。 The code to do so is identical with ListView这样做的代码与ListView相同

It is not <ListView.FooterTemplate> but <ListView.Footer>它不是<ListView.FooterTemplate>而是<ListView.Footer>

<ListView.Footer>
    <StackLayout Margin="5,0,5,5">
        <Label Text="This is my footer" />
    </StackLayout>
</ListView.Footer>

For more info, see: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/customizing-list-appearance#headers-and-footers有关详细信息,请参阅: https : //docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/customizing-list-appearance#headers-and-footers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM