TabelRoot
の拡張メソッドを作成し、よりクリーンなコードを作成できます。
拡張メソッド
public static class Extensions
{
public static TableSection FindSection(this TableRoot root, string title)
{
return root.FirstOrDefault(r => r.Title == title);
}
public static bool AppendAfter(this TableRoot root, string title, TableSection toBeAdded)
{
var section = root.FindSection(title);
if (section != null)
{
var index = root.IndexOf(section);
root.Insert(index + 1, toBeAdded);
}
return false;
}
public static bool AppendBefore(this TableRoot root, string title, TableSection toBeAdded)
{
var section = root.FindSection(title);
if (section != null)
{
var index = root.IndexOf(section);
root.Insert(index, toBeAdded);
}
return false;
}
public static bool Remove(this TableRoot root, string title)
{
var section = root.FindSection(title);
if (section != null)
return root.Remove(section);
return false;
}
public static bool Replace(this TableRoot root, TableSection newSection)
{
var section = root.FindSection(newSection?.Title);
if (section != null)
{
var index = root.IndexOf(section);
root.RemoveAt(index);
root.Insert(index, newSection);
}
return false;
}
}
使用
var root = tableView.Root;
root.Remove("Section 2");
root.AppendAfter("Section 1", toBeAdded: section4 });
root.AppendBefore("Section 2", toBeAdded: section0 });
root.AppendBefore("Section 4", toBeAdded: new TableSection { Title = "Section 3" });
出典
2017-10-02 07:37:56
Ada
感謝。参照の維持に関する問題は、ヘッダーがXAMLで最初に作成されるため、私はそれが不可能であると推測しているということです。しかし、それを見つける方法に関するあなたの提案は完璧です。ありがとう – Alan2