LongListSelector
を使用して日付をグループ化するWindows Phone 8 SDK の質問。私は、文字でグループ化するための AlphaKeyGroup ヘルパー アプローチに精通しています。
同様にロケールを認識している日付について、誰かが同様の書き込みを行った/見たことがありますか? (数字もプラスになります)
LongListSelector
を使用して日付をグループ化するWindows Phone 8 SDK の質問。私は、文字でグループ化するための AlphaKeyGroup ヘルパー アプローチに精通しています。
同様にロケールを認識している日付について、誰かが同様の書き込みを行った/見たことがありますか? (数字もプラスになります)
あなたが言及したMSDNのAlphaKeyGroupの例は、ローカリゼーションのために必要以上に複雑であるため、私もこれに少し苦労しました。あなたがやろうとしているのは、Key という追加のプロパティを持つ新しい List オブジェクトを作成することです。この 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;
}
}
次に、グループ化するオブジェクトの IEnumerable を受け取り、作成したばかりのカスタム リスト オブジェクトのリストを返す CreateGroups というメソッドを作成します。私の実装では、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;
}
これで、グループ化されたリストの素敵なリストができました。素晴らしい!あとは、LongListSelector の itemssource プロパティをこの新しいリストにフックし、JumpListStyle と GroupedHeaderTemplate を定義するだけです。あなたが参照した元の記事には、そのすべての情報が含まれています。
頑張って、Windows Phone の開発をお楽しみください!
あなたと同じ例に行き詰まった後、MSDNのこの例で成功しました。Group.cs ファイルには、文字列で自由に使用できるグループの実装が含まれています。私の推測では、DateTime の別のプロパティを簡単に追加してから、日付ごとにグループ化を試すことができると思います。
さて、AlphaKeyGroup の修正版を使用します。この新しいクラスを StringKeyGroup と呼び、アイテムの最初の文字に基づいてグループを作成します。したがって、AlphaKeyGroup を StringKeyGroup に置き換えるだけです。
この新しい機能は、次のように使用できます。
myLonglistSelector.ItemSource = GroupedItems(myCollection);
....
public ObservableCollection<StringKeyGroup<myClass>> GroupedItems(IEnumerable<myClass> source)
{
return StringKeyGroup<myClass>.CreateGroups(source,
System.Threading.Thread.CurrentThread.CurrentUICulture,
s => s.Name, true);
}
StringKeyGroup.cs のコードは次のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Globalization;
namespace MyNameSpace
{
public class StringKeyGroup<T> : ObservableCollection<T>
{
public delegate string GetKeyDelegate(T item);
public string Key { get; private set; }
public StringKeyGroup(string key)
{
Key = key;
}
public static ObservableCollection<StringKeyGroup<T>> CreateGroups(IEnumerable<T> items, CultureInfo ci, GetKeyDelegate getKey, bool sort)
{
var list = new ObservableCollection<StringKeyGroup<T>>();
foreach (var item in items)
{
var itemKey = getKey(item).Substring(0, 1).ToLower();
var itemGroup = list.FirstOrDefault(li => li.Key == itemKey);
var itemGroupIndex = itemGroup != null ? list.IndexOf(itemGroup) : -1 ;
if (itemGroupIndex == -1)
{
list.Add(new StringKeyGroup<T>(itemKey));
itemGroupIndex = list.Count - 1;
}
if (itemGroupIndex >= 0 && itemGroupIndex < list.Count)
{
list[itemGroupIndex].Add(item);
}
}
if (sort)
{
foreach (var group in list)
{
group.ToList().Sort((c0, c1) => ci.CompareInfo.Compare(getKey(c0), getKey(c1)));
}
}
return list;
}
}
}