【问题标题】:Create Listview C#创建列表视图 C#
【发布时间】:2021-06-03 11:45:00
【问题描述】:

我需要以编程方式创建一个列表视图。此列表视图是从 sql server 填充的。标签的绑定是通过可观察的集合 Shooters 设置的。我需要这个列表视图来更改基于一个名为 rounds 的变量的列数。一切都按预期工作,但是这行代码导致 UI 混乱。

var con = new CustomViewCell(Rounds);
          var connect = await con.GetCellLayout(Rounds); // Returns a grid

          var ShooterDataTemplate = new DataTemplate(() =>
          {
              return new ViewCell { View = connect }; // assigns grid to the viewcell view
          });

我得到具有相同信息的重复行,而其他行缺少它们的标签。 Microsoft 将此称为内联数据模板。下图超链接

bad ui

他们说的第二种选择是这样做..

ShooterListView.ItemTemplate = new DataTemplate(new Typeof(CustomViewCell));

以这种方式完成后,用户界面看起来很正常。但问题是我无法将 rounds 变量传递给 customviewcell,它决定使用哪个函数来设置所有列。

这是完全相同的网格,但通过 typeof(CustomViewCell) 完成

Good UI

在这两种情况下,我都使用完全相同的网格布局设计,唯一的区别是我使用内联代码来创建数据模板,然后是 typeof(customViewCell) 我怎样才能让内联代码不会导致如此奇怪的布局设计,因为我无法将变量传递给 typeof(CustomViewCell)。

下面附上网格代码

public async Task<Grid> Get_Round_7_Cell()
        {
            var column1 = new ColumnDefinition() { Width = new GridLength(15, GridUnitType.Star) };
            var column2 = new ColumnDefinition() { Width = new GridLength(15, GridUnitType.Star) };
            var column3 = new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Star) };
            var column4 = new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Star) };
            var column5 = new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Star) };
            var column6 = new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Star) };
            var column7 = new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Star) };
            var column8 = new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Star) };
            var column9 = new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Star) };

            var smallfont = Device.GetNamedSize(NamedSize.Micro, typeof(Label));

            if (Device.Idiom == TargetIdiom.Tablet || Device.Idiom == TargetIdiom.Phone)
            {
                PaddingLeft = new Thickness(20, 0, 0, 0);
            }
            else
            {
                PaddingLeft = new Thickness(60, 0, 0, 0);
            }


            if (Device.Idiom == TargetIdiom.Tablet || Device.Idiom == TargetIdiom.Phone)
            {
                PaddingRight = new Thickness(0, 0, 20, 0);
            }
            else
            {
                PaddingRight = new Thickness(0, 0, 60, 0);
            }

            var shooterlabel = new Label() { HorizontalTextAlignment = TextAlignment.Start, FontSize = smallfont, Padding = PaddingLeft };
            shooterlabel.SetBinding(Label.TextProperty, new Binding("Shooter_Name"));
            var team_name_label = new Label() { HorizontalTextAlignment = TextAlignment.Center, FontSize = smallfont };
            team_name_label.SetBinding(Label.TextProperty, new Binding("Team_Name"));
            var round1_label = new Label() { HorizontalTextAlignment = TextAlignment.Center, FontSize = smallfont };
            round1_label.SetBinding(Label.TextProperty, new Binding("Round_1"));
            var round2_label = new Label() { HorizontalTextAlignment = TextAlignment.Center, FontSize = smallfont };
            round2_label.SetBinding(Label.TextProperty, new Binding("Round_2"));
            var round3_label = new Label() { HorizontalTextAlignment = TextAlignment.Center, FontSize = smallfont };
            round3_label.SetBinding(Label.TextProperty, new Binding("Round_3"));
            var round4_label = new Label() { HorizontalTextAlignment = TextAlignment.Center, FontSize = smallfont };
            round4_label.SetBinding(Label.TextProperty, new Binding("Round_4"));
            var round5_label = new Label() { HorizontalTextAlignment = TextAlignment.Center, FontSize = smallfont };
            round5_label.SetBinding(Label.TextProperty, new Binding("Round_5"));
            var round6_label = new Label() { HorizontalTextAlignment = TextAlignment.Center, FontSize = smallfont };
            round6_label.SetBinding(Label.TextProperty, new Binding("Round_6"));
            var round7_label = new Label() { HorizontalTextAlignment = TextAlignment.End, FontSize = smallfont, Padding = PaddingRight };
            round7_label.SetBinding(Label.TextProperty, new Binding("Round_7"));

            grid2.ColumnDefinitions.Add(column1);
            grid2.ColumnDefinitions.Add(column2);
            grid2.ColumnDefinitions.Add(column3);
            grid2.ColumnDefinitions.Add(column4);
            grid2.ColumnDefinitions.Add(column5);
            grid2.ColumnDefinitions.Add(column6);
            grid2.ColumnDefinitions.Add(column7);
            grid2.ColumnDefinitions.Add(column8);
            grid2.ColumnDefinitions.Add(column9);

            grid2.Children.Add(shooterlabel, 0, 0);
            grid2.Children.Add(team_name_label, 1, 0);
            grid2.Children.Add(round1_label, 2, 0);
            grid2.Children.Add(round2_label, 3, 0);
            grid2.Children.Add(round3_label, 4, 0);
            grid2.Children.Add(round4_label, 5, 0);
            grid2.Children.Add(round5_label, 6, 0);
            grid2.Children.Add(round6_label, 7, 0);
            grid2.Children.Add(round7_label, 8, 0);

            grid.Children.Add(boxview);
            grid.Children.Add(grid2);


            return grid;
        }

【问题讨论】:

  • 使用 CollectionView,而不是 ListView。 CV 确实依赖于 Cell 概念
  • 我的意思是“不依赖于 Cell 概念”
  • 我试试看。我唯一的其他解决方案是每轮制作 10 个 CustomCellViews。不完全友好。希望收藏视图能正常工作,谢谢!
  • @BrianPalmer 看来你有解决问题的办法。

标签: c# listview xamarin


【解决方案1】:

下面的解决方案。不是最好的,但它完成了工作。我永远无法让内联 DataTemplate 工作,因此我必须为每一轮制作自定义视图单元格,并根据 Rounds 值调用它们。

public async Task<ListView> SetShooterListView(int Rounds)
        {

            var shooterlistview = new ListView() { HorizontalOptions = LayoutOptions.Fill };

            var headergrid = Rounds == 1 ? await Get_Round_1() : Rounds == 2 ? await Get_Round_2() : Rounds == 3 ? await Get_Round_3() : Rounds == 4 ? await Get_Round_4() : Rounds == 5 ? await Get_Round_5() : Rounds == 6 ? await Get_Round_6() : Rounds == 7 ? await Get_Round_7() : Rounds == 8 ? await Get_Round_8() : Rounds == 9 ? await Get_Round_9() : await Get_Round_10();

            var viewcellgrid = Rounds == 1 ? new DataTemplate(typeof(Round_1_View)) : Rounds == 2 ? new DataTemplate(typeof(Round_2_View)) : Rounds == 3 ? new DataTemplate(typeof(Round_3_View)) : Rounds == 4 ? new DataTemplate(typeof(Round_4_View)) : Rounds == 5 ? new DataTemplate(typeof(Round_5_View)) : Rounds == 6 ? new DataTemplate(typeof(Round_6_View)) : Rounds == 7 ? new DataTemplate(typeof(Round_7_View)) : Rounds == 8 ? new DataTemplate(typeof(Round_8_View)) : Rounds == 9 ? new DataTemplate(typeof(Round_9_View)) : new DataTemplate(typeof(Round_10_View));



            


            shooterlistview.Header = headergrid;


            shooterlistview.ItemTemplate = viewcellgrid;
            return shooterlistview;
           

        }

【讨论】:

    猜你喜欢
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多