【问题标题】:Xamarin ListView Grouping IssueXamarin ListView 分组问题
【发布时间】:2019-02-15 06:45:39
【问题描述】:

我在 Xamarin 中使用 ListView 进行分组时遇到问题。我查看的指南数不胜数,但我仍然无法使其正常工作。我可以看到 ViewModel 属性设置正确,但我得到的只是一个空白屏幕。请不要回答另一个指南的链接,因为我可能已经看过它并且没有帮助。我相信我非常接近。

类域:

public class TechInventoryDetail
{
    public int EquipmentInventoryDetailEntityId { get; set; }

    public int TechId { get; set; }

    public string CodeDetail { get; set; }

    public string CodeDescription { get; set; }

    public int Need { get; set; }

    public int Have { get; set; }

    public string ServiceCategory { get; set; }
}

查看模型属性:

private ObservableRangeCollection<Models.Grouping<string, TechInventoryDetail>> _techEquipmentOverview = new ObservableRangeCollection<Models.Grouping<string, TechInventoryDetail>>();

public ObservableRangeCollection<Models.Grouping<string, TechInventoryDetail>> TechEquipmentOverview
{
    get => _techEquipmentOverview;
    set { _techEquipmentOverview = value; RaisePropertyChanged(); }
}

使用视图模型方法设置:

public async Task GetTechEquipmentOverviewAsync()
{
    var result = await _techService.GetTechEquipmentOverviewAsync(false);

    if (result.Success)
    {
        var equipmentGrouped = result.Data.InventoryDetail.GroupBy(c => c.ServiceCategory, c => c);
        var groups = equipmentGrouped.Select(g => new Models.Grouping<string, TechInventoryDetail>(g.Key, g)).ToList();

        if (groups.Any())
        {
            TechEquipmentOverview = new ObservableRangeCollection<Models.Grouping<string, TechInventoryDetail>>();
            TechEquipmentOverview.AddRange(groups);
        }
    }
}

Xaml 列表视图:

<ListView HasUnevenRows="true" 
              IsGroupingEnabled="true"
              GroupDisplayBinding="{Binding Key}"
              ItemsSource="{Binding TechEquipmentOverview}">
        <ListView.GroupHeaderTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <StackLayout Padding="0,5,0,5" Orientation="Vertical">
                            <Label
                                HorizontalTextAlignment="Center"
                                Style="{StaticResource EquipmentGroupHeader}"
                                Text="{Binding Key}" />
                        </StackLayout>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.GroupHeaderTemplate>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid Padding="5">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.Children>
                            <Label
                                Grid.Row="0"
                                Grid.ColumnSpan="4"
                                HorizontalTextAlignment="Center"
                                LineBreakMode="TailTruncation"
                                Style="{StaticResource DefaultSectionHeader}"
                                Text="{Binding CodeDetail}" />

                            <Label
                                Grid.Row="1"
                                Grid.Column="0"
                                Grid.ColumnSpan="2"
                                Margin="0,5,5,5"
                                Style="{StaticResource H1}"
                                Text="Need" />
                            <Label
                                Grid.Row="2"
                                Grid.Column="0"
                                Grid.ColumnSpan="2"
                                Margin="0,15,5,5"
                                LineBreakMode="TailTruncation"
                                Style="{StaticResource H2}"
                                Text="{Binding Need}" />

                            <Label
                                Grid.Row="1"
                                Grid.Column="2"
                                Grid.ColumnSpan="2"
                                Margin="0,5,5,5"
                                Style="{StaticResource H1}"
                                Text="Have" />
                            <Label
                                Grid.Row="2"
                                Grid.Column="2"
                                Grid.ColumnSpan="2"
                                Margin="0,15,5,5"
                                LineBreakMode="TailTruncation"
                                Style="{StaticResource H2}"
                                Text="{Binding Have}" />
                        </Grid.Children>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

【问题讨论】:

  • 你的类 TechInventoryDe​​tail 不应该实现 INotifyPropertyChanged 吗?

标签: c# listview xamarin.forms grouping


【解决方案1】:

在您的 GetTechEquipmentOverviewAsync 方法中,您正在重置 TechEquipmentOverview 可观察集合,而 UI 不知道新列表。

您需要在 ViewModel 中实现 INotifyPropertyChanged通知 UI 列表引用已更改。此外,请确保您使用 Device.BeginInvokeOnMainThread 在主 UI 线程上通知 UI。

【讨论】:

  • 我已按照您的建议更改了我的 ViewModel 属性。它似乎有效,但我必须刷新页面才能显示任何内容。我确定这与您的最后一句话有关,但我不确定您的意思。
  • 有一个主 UI 线程,希望在其上更新 UI 元素。 ListViews 几乎要求您在更新列表时位于 UI 线程上。 iOS 和有时 Android 如果不在主 UI 线程上更新,甚至会出现异常。使用Device.BeginInvokeOnMainThread 将在主线程上运行一个action,从而允许UI 正确更新。希望这会有所帮助
  • 我猜他只需要在构造函数中实例化TechEquipmentOverview列表,调用GetTechEquipmentOverviewAsync时只需Clear即可。
  • 关于主 UI 的事情:只需将您的 AddRange 代码更改为:Device.BeginInvokeOnMainThread(() =&gt; TechEquipmentOverview.AddRange(groups));
猜你喜欢
  • 2020-02-07
  • 2018-01-17
  • 2015-10-28
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
  • 2019-08-10
相关资源
最近更新 更多