我有代码在点击ViewCell时调用事件。在事件处理程序中,我需要知道调用事件的单元格中的Id值。有人可以帮我建议如何获得这个价值。
此外,我想将此值传递给正在打开的categoriesPage。我怎么能这样做?
public class CategoryGroupWordCountVM
{
bool isToggled;
public int Id { get; set; }
public string Name { get; set; }
public bool IsToggled { get; set; }
public int TotalWordCount { get; set; }
}
List<CategoryGroupWordCountVM> categoryGroups;
foreach (var category in categoryGroups) {
var cell = new CategoryGroupTextCell { BindingContext = category };
cell.Tapped += openCategoriesPage;
section.Add(cell);
}
async void openCategoriesPage(object sender, EventArgs e)
{
var ctg = (CategoryGroupTextCell)sender;
var Id = ??
await Navigation.PushAsync(categoriesPage);
}
答案 0 :(得分:0)
您可以使用BindingContext
例如:(假设CategoryPageVM是您在点击事件中导航到的页面的viewmodel类型。)
async void openCategoriesPage(object sender, EventArgs e)
{
var ctg = (CategoryGroupTextCell)sender;
// get id from binding-context
var id = (ctg.BindingContext as CategoryGroupWordCountVM)?.Id;
// construct or get viewmodel for the page you are navigating to
var newPageVM = new CategoryPageVM { CategoryId = id };
// assign viewmodel to page
var categoriesPage = new CategoryPage { BindingContext = newPageVM };
await Navigation.PushAsync(categoriesPage);
}
此外,this link提供了有关在导航过程中传递数据的更多详细信息。