如何在Xamarin.Forms中访问ListView DataTemplate内部的按钮点击?

3
我有一个包含两个动态操作的视图 labelbuttonlistview,我正在尝试在按钮点击时访问按钮单击以转到详细信息页面。
以下是我的自定义 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;
    }
}

希望Navigation.PushAsync(...后面的注释在你实际的代码中是在一行上的?请在这里也将其放在一行上,因为这对编码有很大的影响。 - Kyra
4个回答

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 object)


我尝试了它,而且它能像魔术一样运行,非常感谢Husam Zidan,委托是很棒的。 - Khalid

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


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

我认为这应该适用于您的情况,但我不能确定。我认为您发布的不是您的代码,因为我在您的代码中没有看到任何对se的初始化,并且如评论中所述,如果您真的按照问题中所述放置了代码中的注释,那么注释会导致问题。此外,您没有分享Home类的代码。可能在那一端出了问题。


他不需要对se进行任何初始化。他正在使用Lambda表达式:https://dev59.com/MVjUa4cB1Zd3GeqPUcZk - yiev

0

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接