LongListSelector日期分组列表

use*_*865 3 xaml longlistselector windows-phone-8

Windows Phone 8 SDK问题使用LongListSelector日期分组.我熟悉AlphaKeyGroup帮助方法对字母进行分组.

有没有人完成/看过类似的语言环境感知的日期?(数字也是一个加号)

Gen*_*gen 6

所以我也对这个问题感到困惑,因为你提到的MSDN中的AlphaKeyGroup示例因为本地化而比它需要的更复杂.您要做的是创建一个新的List对象,该对象具有一个额外的属性Key.此Key属性是您分组的名称.在AlphaKeyGroup示例中,它是您所在地区的每个字母.因此,创建自己的继承自List的组对象.

public class TimeKeyGroup<T> : List<T>
{
    /// <summary>
    /// The Key of this group.
    /// </summary>
    public string Key { get; private set; }

    public TimeKeyGroup(string key)
    {
        Key = key;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在创建一个名为CreateGroups的方法,该方法接受要分组的对象的IEnumerable,并返回刚刚创建的自定义列表对象的列表.在我的实现中,我正在对具有TimeStamp属性的Workout对象进行分组.在此方法中,为您想要的每种类型的组键名创建组对象,例如"过去7天"或"过去6个月".然后通过浏览传入的IEnumerable组来填充每个组,并评估每个组以确定它们应该分组的位置.最后将每个分组列表添加到主组列表并返回它.这是我的方法:

public static List<TimeKeyGroup<Workout>> CreateGroups(IEnumerable<Workout> workouts)
    {
        // Create List to hold each item
        List<TimeKeyGroup<Workout>> groupedWorkouts = new List<TimeKeyGroup<Workout>>();

        // Create a TimeKeyGroup for each group I want
        TimeKeyGroup<Workout> LastSeven = new TimeKeyGroup<Workout>("Last Seven Days");
        TimeKeyGroup<Workout> LastTwoWeeks = new TimeKeyGroup<Workout>("Last Two Weeks");
        TimeKeyGroup<Workout> LastMonth = new TimeKeyGroup<Workout>("Last Month");
        TimeKeyGroup<Workout> LastSixMonths = new TimeKeyGroup<Workout>("Last Six Months");
        TimeKeyGroup<Workout> LastYear = new TimeKeyGroup<Workout>("Last Year");
        TimeKeyGroup<Workout> AllTime = new TimeKeyGroup<Workout>("All Time");

        // Fill each list with the appropriate workouts
        foreach (Workout w in workouts)
        {
            if (w.TimeStamp > DateTime.Now.AddDays(-7))
            {
                LastSeven.Add(w);
                continue;
            }
            else if (w.TimeStamp > DateTime.Now.AddDays(-14))
            {
                LastTwoWeeks.Add(w);
                continue;
            }
            else if (w.TimeStamp > DateTime.Now.AddMonths(-1))
            {
                LastMonth.Add(w);
                continue;
            }
            else if (w.TimeStamp > DateTime.Now.AddMonths(-6))
            {
                LastSixMonths.Add(w);
                continue;
            }
            else if (w.TimeStamp > DateTime.Now.AddMonths(-12))
            {
                LastYear.Add(w);
                continue;
            }
            else
            {
                AllTime.Add(w);
            }
        }

        // Add each TimeKeyGroup to the overall list
        groupedWorkouts.Add(LastSeven);
        groupedWorkouts.Add(LastTwoWeeks);
        groupedWorkouts.Add(LastMonth);
        groupedWorkouts.Add(LastSixMonths);
        groupedWorkouts.Add(LastYear);
        groupedWorkouts.Add(AllTime);

        return groupedWorkouts;
    }
Run Code Online (Sandbox Code Playgroud)

现在你有一个很好的分组列表列表.真棒!其余的只是将LongListSelector的itemssource属性挂钩到这个新列表并定义JumpListStyle和GroupedHeaderTemplate.您引用的原始文章包含所有信息.

祝你好运,快乐的Windows Phone开发!