XAML MAUI Bug ListView内容通过使用ListView.GroupHeaderTemplate消失

vltsax25  于 2023-06-27  发布在  其他
关注(0)|答案(2)|浏览(135)

我使用的是一个ListView,其中Binding Group和 IsGroupingEnabled=“True“ 用于标题,但我没有显示任何内容。现在,当我在启动应用程序(在Windows上)后注解掉 * * 的xaml部分<ListView.GroupHeaderTemplate>或注解掉 * * 的先前注解掉的部分<ListView.GroupHeaderTemplate>时,内容会显示出来。当我使用一个普通的 List 而不是 (Top-)Group 并且没有 IsGroupingEnabled 时,它工作得很好。始终显示 * 标签 * 宽度内容 * 测试 *。当我将ListView更改为CollectionView时,我得到了相同的结果。将显示纯列表,但不显示分组列表。

为什么调用页面时,内容没有立即显示,而是在代码中注解(在xaml中)后才显示?

XAML:

<toolkit:Expander Margin="0,0,0,30">
    <toolkit:Expander.Header>
        <Label Text="Tops" Style="{StaticResource labelExpander}"  />
    </toolkit:Expander.Header>
    <Border Stroke="#888"
            StrokeThickness="2"
            StrokeShape="RoundRectangle 10"
            HorizontalOptions="Fill">
        <VerticalStackLayout Margin="16">
            <Label>Test</Label>
            <ListView ItemsSource="{Binding TopGroups}" IsGroupingEnabled="True">
                <ListView.GroupHeaderTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding Name}"
                                   FontSize="18"
                                   FontAttributes="Bold" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.GroupHeaderTemplate>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid Padding="10" >
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="10" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Label Grid.Column="0"    
                                       Text="{Binding Numbering}"/>
                                <Label Grid.Column="2"
                                       Text="{Binding ToText1}"/>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </VerticalStackLayout>
    </Border>
</toolkit:Expander>

MyPage.xaml.cs:

public partial class SitzungsPage
{
    public MyPage(MyPageViewModel myPageViewModel)
    {
        BindingContext = myPageViewModel;
        InitializeComponent();
    }
    //...
}

MyPageViewModel.cs(相关摘录,使用CommunityToolkit.Maui):

[ObservableProperty]
private List<TopGroup> _topGroups;

private void CreateTopGroups()
{
    TopGroups = new List<TopGroup>();
    var tops = new List<Top>();

    foreach (var to in ToDtos)
    {
        var top = new Top();

        switch (to.Status)
        {
            case 1:
            {
                _isPuplicList ??= true;
                if (_isPuplicList is false)
                {
                    TopGroups.Add(new TopGroup("Not Public", tops));
                    tops = new List<Top>();
                    _isPuplicList = true;
                }
                break;
            }
            case 2:
            {
                _isPuplicList ??= false;
                if (_isPuplicList is true)
                {
                    TopGroups.Add(new TopGroup("Public", tops));
                    tops = new List<Top>();
                    _isPuplicList = false;
                }
                break;
            }
        }

        top.ToText1 = to.ToText1;
        top.Numbering = to.Number;
        tops.Add(top);
    }

    if (tops.Count > 0 && _isPuplicList is not null)
    {
        TopGroups.Add(new TopGroup((bool)_isPuplicList ? "Public" : "Not Public", tops));
    }
}

顶级组:

public class TopGroup : List<Top>
{
    public string Name { get; private set; }

    public TopGroup(string name, IEnumerable<Top> tops) : base(tops)
    {
        Name = name;
    }
}

顶部:

public class Top
{
    public string Numbering { get; set; }

    public string ToText1 { get; set; }

    public List<DokumentDto> Documents { get; set; }
}
9rygscc1

9rygscc11#

现在这个问题已经被编辑,以显示TopGroup是如何填充的,这个问题更容易被发现。
一个有效的解决方案是在列表填满之前不设置属性。在局部变量中创建列表;在填充本地列表后设置属性:

var topGroups = new List<TopGroup>();

...
   topGroups.Add(...);   <-- everywhere that an item is added.
...

TopGroups = topGroups;   // Finally, set the property.

从头开始创建列表时,这可能比清除属性列表并一次添加一个属性(这可能导致多个更新事件)的性能更好。或者差异可以忽略不计,这取决于何时调用,列表中有多少项,每个项的显示复杂度。

dwbf0jvd

dwbf0jvd2#

我已经能够通过在ViewModel中将ObservableProperty更改为ObservableCollection来解决这个问题:

[ObservableProperty]
private List<TopGroup> _topGroups;

public ObservableCollection<TopGroup> TopGroups { get; private set; } = new();

TopGroups = new List<TopGroup>();

TopGroups.Clear();

问题是当TopGroups设置为new()时会触发更改事件。所以是空的。

相关问题