分组 LongListSelector:标题出现,项目不出现

2024-03-19

C# 中的 WinPhone 8 项目。我正在尝试填充分组列表。组标题出现,但项目不出现。相关代码是:

class MyPage
{
    public class Group : IGrouping<string, string>
    {
        public string Title{get;set;}
        public string[] Items;

        public string Key
        {
            get { return Title; }
        }

        public IEnumerator<string> GetEnumerator()
        {
            return (Items as IEnumerable<string>).GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Items.GetEnumerator();
        }
    }

    private Group[] m_ItemGroups =
        {
            new Group(){Title = "A", Items = new string[] {"A", "ASA"}},
            new Group(){Title = "X", Items = new string[] {"X", "XX"}},
        };

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        TheList.ItemsSource = m_ItemGroups;
    }
}

以及 XAML:

<phone:LongListSelector
        x:Name="TheList"
        Grid.Row="1"
        IsGroupingEnabled="True"
        SelectionChanged="OnSelChanged"
        >

        <phone:LongListSelector.GroupHeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}"
                   Style="{StaticResource PhoneTextGroupHeaderStyle}"
                   Foreground="{StaticResource PhoneForegroundBrush}" />
            </DataTemplate>
        </phone:LongListSelector.GroupHeaderTemplate>

        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal">
                    <TextBlock Text="Hello world" TextWrapping="Wrap" Width="345"/>
                </StackPanel>

            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>

    </phone:LongListSelector>

两者都没有GetEnumerator()方法被调用。这Keygetter 也从未被调用。看起来该列表无法识别我的Group类作为字符串的集合。请问,这是怎么回事?

项目模板很好。当我将列表更改为非分组时,我看到两个带有虚拟文本的项目。

更换string因为具有自定义类的项目类型没有帮助。


Seva 是正确的,Microsoft 更改了您分配给ItemsSource of a LongListSelector在分组模式下。

您需要转换您使用的任何类来对继承的项目进行分组IEnumerable<T>只是继承List<T>.

See 这里有答案的完整描述 http://www.geekchamp.com/articles/the-new-longlistselector-control-in-windows-phone-8-sdk-in-depth

实际上非常简单,这是您可以与WP8LongListSelector 看起来像:

    public class Group<T> : List<T>
{
    public Group(char name, IEnumerable<T> items) : base(items)
    {
        this.Letter = name;

    }



    public char Letter
    {
        get;
        set;
    }


}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

分组 LongListSelector:标题出现,项目不出现 的相关文章

随机推荐