我有listview
有两个动态操作label
和button
的视图,我试图访问按钮点击按钮点击进入详细信息页面。
是我的自定义ViewCell
protected override async void OnAppearing()
{
listClass.ItemsSource = list;
listClass.ItemTemplate = new DataTemplate(typeof(ItemTemplateViewCell ));
}
public class ItemTemplateViewCell : ViewCell
{
Label NameLbl = new Label();
StackLayout sLayout = new StackLayout ();
Button btnViewcell = new Button {Text = "Show class details"};
public ItemTemplateViewCell()
{
NameLbl.SetBinding(Label.TextProperty, "Name");
sLayout.Children.Add(NameLbl);
btnViewcell.Clicked += (s, e) =>
{
// Navigation.PushAsync(new Home()); //I can not using this line
// does not exist in the current context, why cant i navigate to
// another page from inside datatemplate in List view
};
sLayout.Children.Add(btnViewcell);
this.View = sLayout;
}
}
答案 0 :(得分:3)
您可以通过构造函数将导航传递给ViewCell:
public class ItemTemplateViewCell : ViewCell
{
// Navigation Mermber
INavigation MyNavigation;
Label NameLbl = new Label();
StackLayout sLayout = new StackLayout ();
Button btnViewcell = new Button {Text = "Show class details"};
public ItemTemplateViewCell(INavigation navigation)
{
MyNavigation = navigation;
NameLbl.SetBinding(Label.TextProperty, "Name");
sLayout.Children.Add(NameLbl);
btnViewcell.Clicked += ButtonShowDetails_Clicked;
sLayout.Children.Add(btnViewcell);
this.View = sLayout;
}
private void ButtonShowDetails_Clicked(object sender, EventArgs e)
{
MyNavigation.PushAsync(new Home());
}
}
然后通过委托函数
传递导航 protected override async void OnAppearing()
{
listClass.ItemsSource = list;
listClass.ItemTemplate = new DataTemplate(Function) ;
}
public object Function()
{
return new ItemTemplateViewCell (Navigation);
}
然后您可以在ViewCell中访问Navigation对象
答案 1 :(得分:1)
您只需使用CommandParameterProperty
绑定:
示例:
ListViewClass.ItemTemplate = new DataTemplate(() =>
{
var GoHomeButton = new Button { Text = "Go Home", HorizontalOptions = LayoutOptions.StartAndExpand };
GoHomeButton.SetBinding(Button.CommandParameterProperty, new Binding("Name"));
GoHomeButton.Clicked += Gohome;
//return new view cell
}
private void Gohome(object sender, EventArgs e)
{
Navigation.PushAsync(new Home());
}
有关详细信息,请查看以下链接:
http://www.c-sharpcorner.com/blogs/handling-child-control-event-in-listview-using-xamarinforms1
答案 2 :(得分:0)
我认为出错的是您在课程初始化中立即执行操作。你应该把它分开,如下所示。另一个问题是您在方法之外进行变量初始化。这可能没问题,但我更喜欢以下代码:
public class ItemTemplateViewCell : ViewCell
{
Label nameLbl;
StackLayout sLayout;
Button btnViewcell;
public ItemTemplateViewCell()
{
nameLbl = new Label()
sLayout = new StackLayout ()
btnViewcell = new Button {Text = "Show class details"};
NameLbl.SetBinding(Label.TextProperty, "Name");
sLayout.Children.Add(NameLbl);
btnViewcell.Clicked += OnButtonClicked;
sLayout.Children.Add(btnViewcell);
this.View = sLayout;
}
void OnButtonClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new Home());
}
}
我认为这应该适合你的情况,但我不能确定。我不认为你发布的是你的代码一对一,因为我没有在你的代码中看到s
和e
的任何初始化,并且在评论中提到代码中的注释会导致问题,如果你真的把它作为问题。此外,您不共享Home
类的代码。那方面可能有些错误。
答案 3 :(得分:0)
Navigation
在ViewCell
内无法使用,这意味着您无法直接从ViewCell
访问它。但这应该有效:
App.Current.MainPage.Navigation.PushAsync(new Home());
整个代码:
protected override async void OnAppearing()
{
listClass.ItemsSource = list;
listClass.ItemTemplate = new DataTemplate(typeof(ItemTemplateViewCell ));
}
public class ItemTemplateViewCell : ViewCell
{
Label NameLbl = new Label();
StackLayout sLayout = new StackLayout ();
Button btnViewcell = new Button {Text = "Show class details"};
public ItemTemplateViewCell()
{
NameLbl.SetBinding(Label.TextProperty, "Name");
sLayout.Children.Add(NameLbl);
btnViewcell.Clicked += (s, e) =>
{
App.Current.MainPage.Navigation.PushAsync(new Home());
};
sLayout.Children.Add(btnViewcell);
this.View = sLayout;
}
}