【问题标题】:programatically binding a listview in xamarin c#在 xamarin c# 中以编程方式绑定列表视图
【发布时间】:2019-08-16 04:19:42
【问题描述】:

我想在 Xamarin 便携版中创建一个列表视图, 列表视图的项目来源是List<String>,我的列表视图数据模板是由这个函数准备的,

private DataTemplate createItemtemplate()
{
    try
    {
        Label lbl_binding = new Label()
        {
            TextColor = Color.Red,
            FontSize = 16,
            VerticalTextAlignment = TextAlignment.Center,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
        };
        lbl_binding.SetBinding(Label.TextProperty, ".");
        StackLayout stkBottom = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            VerticalOptions = LayoutOptions.Center,
            Padding = new Thickness(0),
        };
        stkBottom.Children.Add(lbl_binding);
        ViewCell vc = new ViewCell() {
            View = stkBottom
        };

        DataTemplate Dp = new DataTemplate(() =>
        {
            return vc;
        });
        return Dp;
    }
    catch (Exception ex)
    {
        return null;
    }
}

现在,我的列表视图已填充,但所有标签都填充了最后一项,我的意思是正确填充了项数,但所有项仅填充了最后一项。 我在这里做错了什么?

lstAdmin = new ListView()
{
    ItemTemplate = createItemtemplate(),
};
lstadmin.Itemsource = source;

【问题讨论】:

    标签: c# .net xamarin xamarin.forms


    【解决方案1】:

    将您的列表视图从 List 更改为 ObservableCollection。这应该可以完成工作。

    【讨论】:

    • 请编辑您的问题并告诉我您是如何填写列表的。
    • 以及如何将此数据模板设置为列表视图
    • @Thava 我猜listview.ItemsSource = source 是问题所在。您只需定义一次 ItemsSource 属性,然后将您的项目添加到其中。非常推荐你使用它作为绑定,遵循 MVVM 模式,避免这种挣扎,但我们可以处理它。尝试在您的构造函数中设置listview.ItemsSource = new List<string>(),然后像((List<string>)listview.ItemsSource).Add("stringToAdd") 一样将您的元素添加到其中
    • 感谢@Diego,但同样的问题仍然存在
    • 这到底是怎么回事,伙计?!最后一次尝试:发布所有代码 xaml、代码隐藏和外部类(如果有)。我会尝试在这里重现同样的错误
    【解决方案2】:

    下面的代码对你很有参考价值

    参加班级成员:

       public class Dummy
        {
    
        public string Id { get; set; }
        public string Img { get; set; }
        public string Title { get; set; }
        public string SubTitle { get; set; }
        public string Count { get; set; }
        public string Status { get; set; }
    
        }
    

    创建一个 ObservableCollectionList:

       ObservableCollection<Dummy> productItems = new 
       ObservableCollection<Dummy>();
    

    将项目添加到 ObservableCollectionList:

       productItems.Add(new Dummy() { Name = "0", Img = "Avatar.png", 
       Title = "Total Books", SubTitle = "Desc", Count = "50", Status = 
       "Total" });
    
       productItems.Add(new Dummy() { Id = "1", Img = "Avatar.png", Title 
       = "Out of Stock Books", SubTitle = "Desc", Count = "40", Status = 
       "OutStock" });
    

    声明 ListView:

       ListView listview = new ListView()
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                SeparatorVisibility = SeparatorVisibility.None,
                RowHeight = 30,
            };
    listview.ItemTemplate = new DataTemplate(typeof(cell));
    listview.ItemSelected += listviewItemSelected;
    

    使用 ViewCell 并在 ViewCell 中设计您的 UI 并分配绑定

        public class cell : ViewCell
        {
            public cell()
            {
                Image img = new Image()
                {
                    VerticalOptions = LayoutOptions.StartAndExpand,
                };
    
                img.SetBinding(Image.SourceProperty, new Binding("Img"));
    
                Label lbltitle = new Label()
                {
                    VerticalOptions = LayoutOptions.Start,
                    TextColor = Color.Black
                };
    
                lbltitle.SetBinding(Label.TextProperty, new 
               Binding("Title"));
    
                Label lbldesc = new Label()
                {
                    VerticalOptions = LayoutOptions.Start,
                    TextColor = Color.Black
                };
    
                lbldesc.SetBinding(Label.TextProperty, new 
                Binding("SubTitle"));
    
                StackLayout lblstack = new StackLayout()
                {
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Children = { lbltitle, lbldesc },
                };
                BoxView boxEdit = new BoxView()
                {
                    VerticalOptions = LayoutOptions.Start,
                    HorizontalOptions = LayoutOptions.End,
                    Color = Color.Black,
                    HeightRequest = 20,
                    WidthRequest = 10
                };
    
                tapGestureEdit.Tapped += tapGestureEditTapped;
                Label lblCount = new Label()
                {
                    VerticalOptions = LayoutOptions.CenterAndExpand,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    TextColor = Color.Black
                };
                lblCount.SetBinding(Label.TextProperty, new 
                Binding("Count"));
                StackLayout stackCount = new StackLayout()
                {
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    HorizontalOptions = LayoutOptions.EndAndExpand,
                    Children = { boxEdit, lblCount },
                };
                StackLayout stackMain = new StackLayout()
                {
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Children = { img, lblstack, stackCount },
                    Orientation = StackOrientation.Horizontal,
                    Margin = new Thickness(10, 10, 10, 10)
                };
                View = stackMain;
            }
         }
    

    【讨论】:

    • 谢谢,不幸的是,创建一个视图单元显示一些错误,我已经在控件中应用了它并添加了一个渲染器
    【解决方案3】:
    public class AdminCell : ViewCell
    {
        public AdminCell()
        {
            Label lbl_binding = new Label()
            {
                TextColor = Color.FromRgb(30, 144, 255),
                FontSize = 16,
                VerticalTextAlignment = TextAlignment.Center,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
            };
            lbl_binding.SetBinding(Label.TextProperty, ".");
            StackLayout stkBottom = new StackLayout()
            {
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                VerticalOptions = LayoutOptions.Center,
                Padding = new Thickness(0),
            };
            stkBottom.Children.Add(lbl_binding);
            View = stkBottom;
        }
    }
    

    此代码为我工作删除了模板并使用此单元格,我仍然不明白为什么数据模板不起作用

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,经过一些测试,我想出了如何不总是得到最后一项。

      您应该像这样将标签创建和所有其他要放入 ItemTemplate 的元素放在 DataTemplate 的 lambda 中(示例代码):

      DataTemplate itemTemplate = new DataTemplate(() =>
                  {
                      Label label = new Label()
                      {
                          Margin = new Thickness(45, 0, 0, 0),
                          HorizontalOptions = LayoutOptions.Start,
                          FontSize = (double)Xamarin.Forms.Application.Current.Resources["BodyFontSize"],
                          HeightRequest = 20
                      };
                      label.SetBinding(Label.TextProperty, "Name");
      
                      ViewCell templateCell = new ViewCell()
                      {
                          View = label
                      };
      
                      return templateCell;
                  });
      

      希望有所帮助(在我的情况下有所帮助)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-14
        • 2014-05-28
        • 2011-09-14
        • 2019-08-17
        相关资源
        最近更新 更多