私は phonetoolkit longlistselector とmsdnのサンプルに従っています。アプリケーション ページに showmore 機能を追加しようとしています。通常、longlistselector のように、著者をアルファベット順に表示し、グループごとに [詳細を表示] ボタンを表示します。
問題: ここでの問題は、[もっと表示] ボタンを押して新しい作成者を追加できず、ロングリスト セレクターでこれを更新できないことです。Alphakeygroup クラスに作成者を追加する際に問題が発生しています (上記のリンクにあります)。これどうやってするの?
これは私の AlphaKeyGroup クラスです
public class AlphaKeyGroup<T> : List<T>
{
/// <summary>
/// The delegate that is used to get the key information.
/// </summary>
/// <param name="item">An object of type T</param>
/// <returns>The key value to use for this object</returns>
public delegate string GetKeyDelegate(T item);
/// <summary>
/// The Key of this group.
/// </summary>
public string Key { get; private set; }
/// <summary>
/// Public constructor.
/// </summary>
/// <param name="key">The key for this group.</param>
public AlphaKeyGroup(string key)
{
Key = key;
}
/// <summary>
/// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
/// </summary>
/// <param name="slg">The </param>
/// <returns>Theitems source for a LongListSelector</returns>
private static List<AlphaKeyGroup<T>> CreateGroups(SortedLocaleGrouping slg)
{
List<AlphaKeyGroup<T>> list = new List<AlphaKeyGroup<T>>();
foreach (string key in slg.GroupDisplayNames)
{
list.Add(new AlphaKeyGroup<T>(key));
}
return list;
}
/// <summary>
/// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
/// </summary>
/// <param name="items">The items to place in the groups.</param>
/// <param name="ci">The CultureInfo to group and sort by.</param>
/// <param name="getKey">A delegate to get the key from an item.</param>
/// <param name="sort">Will sort the data if true.</param>
/// <returns>An items source for a LongListSelector</returns>
public static List<AlphaKeyGroup<T>> CreateGroups(IEnumerable<T> items, CultureInfo ci, GetKeyDelegate getKey, bool sort)
{
SortedLocaleGrouping slg = new SortedLocaleGrouping(ci);
List<AlphaKeyGroup<T>> list = CreateGroups(slg);
foreach (T item in items)
{
int index = 0;
if (slg.SupportsPhonetics)
{
//check if your database has yomi string for item
//if it does not, then do you want to generate Yomi or ask the user for this item.
//index = slg.GetGroupIndex(getKey(Yomiof(item)));
}
else
{
index = slg.GetGroupIndex(getKey(item));
}
if (index >= 0 && index < list.Count)
{
list[index].Add(item);
}
}
if (sort)
{
foreach (AlphaKeyGroup<T> group in list)
{
group.Sort((c0, c1) => { return ci.CompareInfo.Compare(getKey(c0), getKey(c1)); });
}
}
return list;
}
}
これは私のxamlページです。showmore
ここでボタンをクリックすると、morecommand
クラスの実行機能に成功しました。関数に著者を追加してから、longlistselector で更新するにはどうすればよいですか? 基本的に、AlphaKeyGroup クラスを操作するのは難しいと感じています。
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:LongListSelector x:Name="AuthorsList" IsGroupingEnabled="true" HideEmptyGroups="True" LayoutMode="List"
ItemsSource="{Binding Authors}"
ListHeaderTemplate="{StaticResource movieListHeader}"
GroupHeaderTemplate="{StaticResource movieGroupHeader}"
ItemTemplate="{StaticResource movieItemTemplate}"
JumpListStyle="{StaticResource MoviesJumpListStyle}">
<!-- The group footer template, for groups in the main list -->
<phone:LongListSelector.GroupFooterTemplate>
<DataTemplate>
<Button DataContext="{Binding}" Content="Show More Records"
Command="{StaticResource moreCommand}" CommandParameter="{Binding}"/>
</DataTemplate>
</phone:LongListSelector.GroupFooterTemplate>
</phone:LongListSelector>
</Grid>
</Grid>
私の xaml.cs ファイル
public CategoryFilter()
{
InitializeComponent();
authorsviewmodel = new AuthorsViewModel();
LoadAuthors();
}
private void LoadAuthors()
{
List<Author> movies = new List<Author>();
authorsviewmodel.GetAllAuthors();
var Authorsgroup = AlphaKeyGroup<Author>.CreateGroups(authorsviewmodel.AuthorsList, System.Threading.Thread.CurrentThread.CurrentUICulture, (Author s) => { return s.AuthorName; }, true);
AuthorsList.ItemsSource = Authorsgroup;
}
Morecommands クラス
public class MoreCommand : ICommand
{
#region ICommand Members
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
AlphaKeyGroup<Author> list = parameter as AlphaKeyGroup<Author>;
Author item = new Author();
item.AuthorName = "BAIG123";
list.Add(item);
AuthorsViewModel authorviewmodel=new AuthorsViewModel();
authorviewmodel.Authors = parameter as List<AlphaKeyGroup<Author>>;
authorviewmodel.Authors.Add(list);
}
#endregion
}