ListView 内容未出现 - Xamarin Forms

问题描述 投票:0回答:1

我试图显示周列表,每周可以有多个事件和恢复(

GroupedWeeksModel
类)。问题是
<ListView.ItemTemplate>
中的内容未显示,并且
<ListView.GroupHeaderTemplate>
内容以错误的文本颜色显示。

EventsPage.xaml 文件:

<ScrollView>
    <StackLayout Orientation="Vertical">
        <ListView BackgroundColor="DeepPink" IsGroupingEnabled="True" ItemsSource="{Binding GroupedWeeks}" GroupDisplayBinding="{Binding DateString}" SeparatorVisibility="None" VerticalOptions="Start" x:Name="weeksList" HasUnevenRows="True">
            <ListView.GroupHeaderTemplate>
                <DataTemplate>
                    <ViewCell Height="60">
                        <StackLayout VerticalOptions="Center">
                            <Label Text="{Binding DateString}" TextColor="White" VerticalOptions="Center"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.GroupHeaderTemplate>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell Height="75">
                        <StackLayout HeightRequest="200" WidthRequest="200">
                                    
                            <Label> Why isn't this text appearing? </Label>

                            <StackLayout BindableLayout.ItemsSource="{Binding Events}">
                                <BindableLayout.ItemTemplate>
                                    <DataTemplate>
                                        <ui:MaterialCard HeightRequest="70" Margin="20, 0, 20, 5" Padding="0" BackgroundColor="Red">
                                            <StackLayout Padding="10, 0, 0, 0" HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                                                <StackLayout Orientation="Vertical" WidthRequest="30" VerticalOptions="Center" HorizontalOptions="Center">
                                                    <ui:MaterialLabel TextColor="White" Text="{Binding ShortMonth}"/>
                                                    <ui:MaterialLabel FontSize="Large" TextColor="White" Text="{Binding Day}"/>
                                                </StackLayout>
                                                <StackLayout Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="StartAndExpand">
                                                    <ui:MaterialLabel LineBreakMode="TailTruncation" TextColor="White" Text="{Binding Name}"/>
                                                    <ui:MaterialLabel TextColor="White" Text="{Binding FullDate}"/>
                                                </StackLayout>
                                            </StackLayout>
                                        </ui:MaterialCard>
                                    </DataTemplate>
                                </BindableLayout.ItemTemplate>
                            </StackLayout>

                            <StackLayout BindableLayout.ItemsSource="{Binding Recoveries}">
                                <BindableLayout.ItemTemplate>
                                    <DataTemplate>
                                        <ui:MaterialCard HeightRequest="70" Margin="20, 0, 20, 5" Padding="0" BackgroundColor="Green">
                                            <StackLayout Padding="10, 0, 0, 0" HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                                                <StackLayout Orientation="Vertical" WidthRequest="30" VerticalOptions="Center" HorizontalOptions="Center">
                                                    <ui:MaterialLabel TextColor="White" Text="{Binding ShortMonth}"/>
                                                    <ui:MaterialLabel FontSize="Large" TextColor="White" Text="{Binding Day}"/>
                                                </StackLayout>
                                                <StackLayout Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="StartAndExpand">
                                                    <ui:MaterialLabel LineBreakMode="TailTruncation" TextColor="White" Text="{Binding RecoveryType.Name}"/>
                                                    <ui:MaterialLabel TextColor="White" Text="{Binding Date}"/>
                                                </StackLayout>
                                            </StackLayout>
                                        </ui:MaterialCard>
                                    </DataTemplate>
                                </BindableLayout.ItemTemplate>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell> 
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ScrollView>

EventsPage.xaml.cs 文件:

public partial class EventsPage : ContentPage
{
    public EventsPage()
    {
        InitializeComponent();
        BindingContext = new EventsViewModel();
    }
}

事件视图模型:

public class EventsViewModel : BaseViewModel
{
    public ObservableCollection<GroupedWeeksModel> GroupedWeeks { get; set; } = new ObservableCollection<GroupedWeeksModel>();

    public EventsViewModel()
    {
        Device.BeginInvokeOnMainThread(async () => await InitializeAsync());
    }

    public async Task InitializeAsync()
    {
        GroupedWeeks = await LoadEventsAndRecoveries();
    }
}

public class GroupedWeeksModel
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string DateString => // Some Logic
    public ObservableCollection<EventResponse> Events { get; set; } = new ObservableCollection<EventResponse>();
    public ObservableCollection<RecoveryResponse> Recoveries { get; set; } = new ObservableCollection<RecoveryResponse>();
}
c# xaml listview xamarin.forms
1个回答
0
投票

我设法找到了我的问题,我只需要组合

EventResponse
RecoveryResponse
模型并更改
GroupedWeeksModel
,如下所示:

public class GroupedWeeksModel : ObservableCollection<EventAndRecoveryResponse>
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string DateString => // Some Logic
}
© www.soinside.com 2019 - 2024. All rights reserved.