You need to enable JavaScript to run this app. C#基于rounds实现ListView动态列:DataTemplate传参与UI异常
优惠活动
大模型
产品
解决方案
定价
更多

C#基于rounds实现ListView动态列:DataTemplate传参与UI异常

Xamarin.Forms 动态列实现相关问题

我需要通过编程方式创建ListView,从SQL Server加载数据,列表中的所有标签均通过ObservableCollection<Shooters>设置绑定。核心需求是根据名为rounds的变量动态调整ListView的列数。目前其余功能均符合预期,但以下代码会导致UI布局错乱:

var con = new CustomViewCell(Rounds);
var connect = await con.GetCellLayout(Rounds); // 返回组装完成的Grid布局

var ShooterDataTemplate = new DataTemplate(() =>
{
    return new ViewCell { View = connect }; // 将Grid布局赋值给ViewCell的视图
});

采用这种微软定义为内联数据模板的实现方式时,会出现包含相同信息的重复行,部分行还会缺失对应标签,整体UI布局错位。

微软给出的第二种实现方式如下:

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

采用这种方式实现后UI显示正常,但存在明确问题:无法将rounds变量传递给CustomViewCell,而该变量是决定调用哪个方法生成对应列布局的核心参数。

两种场景下使用的网格布局设计完全一致,唯一区别是分别采用内联代码创建DataTemplate、以及指定自定义单元格类型的方式实现模板。由于指定类型的实现方式无法传入自定义的rounds参数,想了解如何解决内联代码方式的UI布局错乱问题。


7轮列网格生成相关代码

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

内容的提问来源于stack exchange,提问作者Brian Palmer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.01 04:45:01