如何访问按钮单击Xamarin.Forms中的listview数据模板?
我有listview
有两个视图动态操作label
和button
,我试图访问按钮点击进入按钮点击的详细信息页面。下面如何访问按钮单击Xamarin.Forms中的listview数据模板?
是我的自定义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;
}
}
你可以通过构造函数传递Navication到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());
}
}
然后通过委托功能通过Navication
protected override async void OnAppearing()
{
listClass.ItemsSource = list;
listClass.ItemTemplate = new DataTemplate(Function) ;
}
public object Function()
{
return new ItemTemplateViewCell (Navigation);
}
然后,您可以访问导航对象ViewCell
我试过了,它的工作就像一个魅力,非常感谢Husam Zidan,代表非常棒 – Immortal
我的想法是怎么了?是你在类的初始化立即执行动作。你应该像下面这样分割它。另一个问题是你在方法之外进行变量的初始化。这可能会去好了,但我更喜欢下面的代码:
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
类的代码。这可能是错误的。
他不需要's'和'e'的任何初始化。他使用的是Lambda表达式:https://stackoverflow.com/questions/6531970/what-is-the-meaning-of-se-in-the-code – yiev
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;
}
}
只要你能够使用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
我希望背后的注释'Navigation.PushAsync(...'是在你的实际代码中的一行?请把它放在一行,然后在这里,因为这确实在编码上有很大的区别 – Kyra