【问题标题】:How to access button click inside listview datatemplate in Xamarin.Forms?如何在 Xamarin.Forms 中访问列表视图数据模板内的按钮单击?
【发布时间】:2018-02-22 06:44:03
【问题描述】:

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

【问题讨论】:

  • 我希望Navigation.PushAsync(... 后面的注释在您实际代码中的一行?请把它放在一行然后在这里,因为这确实在编码方面有很大的不同

标签: c# xamarin.forms cross-platform portable-class-library


【解决方案1】:

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

然后通过委托函数传递导航

    protected override async void OnAppearing()
    {
       listClass.ItemsSource = list;
       listClass.ItemTemplate = new DataTemplate(Function) ; 
    }

    public object Function()
    {
        return new ItemTemplateViewCell (Navigation);
    }

然后就可以访问ViewCell中的Navigation对象了

【讨论】:

  • 我试过了,效果很好,非常感谢 Husam Zidan,代表们太棒了
【解决方案2】:

您只需将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

【讨论】:

    【解决方案3】:

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

    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 的任何初始化,并且如 cmets 中所述,如果您真的在代码中的注释会导致问题把它放在问题中。此外,您不共享 Home 类的代码。最后可能有问题。

    【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      相关资源
      最近更新 更多