XAML Maui如何在标签中显示字符串列表?

bwntbbo3  于 2023-05-21  发布在  其他
关注(0)|答案(1)|浏览(139)

我有一个类 SearchPageResultViewModel 的对象 SearchResults,其属性 Highlights 是一个字符串列表。列表条目的数量各不相同。我想用xaml显示所有的值。列表中的字符串有html标签,所以我需要使用带有属性TextType=“Html”的标签来正确显示它们。

如何在xaml中遍历列表并在标签中显示内容?

视图模型:

using CommunityToolkit.Mvvm.ComponentModel;

namespace MyProject.ViewModels;

public partial class SearchPageResultViewModel : ObservableObject
{
   [ObservableProperty]
   private ObservableCollection<SearchPageResultViewModel> _searchResults;
}

型号:

using CommunityToolkit.Mvvm.ComponentModel;

namespace MyProject.ViewModels;

public partial class SearchPageResultViewModel : ObservableObject
{
    public DateTime Date { get; set; }
    public List<string> Highlights { get; set; } = new();
}

示例1:

<CollectionView Grid.Row="1" ItemsSource="{Binding SearchResults}" >
         <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="viewModels:SearchPageResultViewModel">
                <Grid RowDefinitions="auto, auto, *" ColumnDefinitions="Auto" >
                    <HorizontalStackLayout Grid.Row="0" >
                        <Label Text="(" />
                        <Label Text="{Binding Date, StringFormat='{0:dd.MM.yyyy}'}" />
                        <Label Text=")" />
                    </HorizontalStackLayout>
            <!-- I'm looking for way to display the list Highlights.
                                           My pseudo code: -->
                    foreach(string highlight in Highlights)
                    {
                       <Label TextType="Html" Grid.Row="1"
                           Text="{Binding highlight}" />                        
                    }
                </Grid>
            </DataTemplate>
         </CollectionView.ItemTemplate>
        </CollectionView>
gwo2fgha

gwo2fgha1#

添加 StackLayout width BindableLayout.ItemsSource="{Binding..}
但是包含html标签的绑定文本仍然存在一个问题。它们未正确显示。https://github.com/dotnet/maui/issues/13015应该在.NET 8中修复。

<CollectionView Grid.Row="1" ItemsSource="{Binding SearchResults}" >
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="viewModels:SearchPageResultViewModel">
            <Grid RowDefinitions="auto, auto, *" ColumnDefinitions="Auto, *" Margin="0,0,0,0" >
                <Label Grid.ColumnSpan="2" Text="{Binding Doname}" FontSize="30" />
                <HorizontalStackLayout Grid.Row="1" Grid.ColumnSpan="2">
                    <Label Text="(" />
                    <Label Text="{Binding Date, StringFormat='{0:dd.MM.yyyy}'}" />
                    <Label Text=")" />
                </HorizontalStackLayout>

                <StackLayout Grid.Row="2" Grid.ColumnSpan="2" 
                             BindableLayout.ItemsSource="{Binding Highlights}" 
                             Orientation="Vertical" >
                    <BindableLayout.ItemTemplate>
                        <DataTemplate>
                            <Label TextType="Html" Text="{Binding}" />
                        </DataTemplate>
                    </BindableLayout.ItemTemplate>
                </StackLayout>

            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

相关问题