如何在 Xamarin.Forms 中访问列表视图数据模板中的按钮单击?

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

我认为出了问题的是您在 class 的初始化中立即执行了一个操作。你应该像下面这样把它分开。另一个问题是您在方法之外对变量进行初始化。这可能没问题,但我更喜欢下面的代码:

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 的任何初始化,并且如评论中所述,您的代码中的评论会导致问题,如果你真的把它放在问题中。此外,您不共享 Home class 的代码。在那一端可能有问题。

NavigationViewCell 中不可用,这意味着您无法直接从 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;
    }
}

您可以通过构造函数将导航传递给 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());
    }

有关详细信息,请查看以下内容 Link:

http://www.c-sharpcorner.com/blogs/handling-child-control-event-in-listview-using-xamarinforms1