繁体   English   中英

如何在Xamarin.Forms中访问listview datatemplate内的按钮?

[英]How to access button click inside listview datatemplate in Xamarin.Forms?

我有listview有两个视图动态labelbutton ,我试图访问按钮单击按钮单击进入详细信息页面。

下面是我的自定义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;
    }
}

您可以通过构造函数将导航传递给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对象

只需您可以使用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

我认为出错的是你在类的初始化中立即执行一个动作。 你应该把它分开,如下所示。 另一个问题是您在方法之外进行变量初始化。 这可能没问题,但我更喜欢以下代码:

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()); 
    }
}

我认为这应该适合你的情况,但我不能确定。 我不认为你发布的是你的代码一对一,因为我没有在你的代码中看到se任何初始化,并且e评论中所提到的,如果你真的把它放在代码中,那么代码中的注释会引起问题这个问题。 此外,您不共享Home类的代码。 在那一端可能有些不对劲。

ViewCell内部无法Navigation ,这意味着您无法直接从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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM