gpt4 book ai didi

listview - Xamarin Forms ListView - 异常?

转载 作者:行者123 更新时间:2023-12-05 01:15:36 25 4
gpt4 key购买 nike

我有一个非常简单的 ListView,它被编程为显示具有三个属性的列表。每当我尝试将集合绑定(bind)到该 ListView 时,我都会收到 InvalidCast 异常。我不知道这有什么问题!

下面是我的 XAML

<ContentPage.Content>
<StackLayout>
<ListView x:Name="listAttendanceTiming" HasUnevenRows="True">
<ListView.HeaderTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<Label Text="From Time" HorizontalTextAlignment="Start" FontSize="14"
Grid.Row="0" Grid.Column="0" />

<Label Text="To Time" HorizontalTextAlignment="Start" FontSize="14"
Grid.Row="0" Grid.Column="1" />

<Label Text="Remarks" HorizontalTextAlignment="Start" FontSize="14"
Grid.Row="0" Grid.Column="2" />

</Grid>
</ViewCell>
</DataTemplate>
</ListView.HeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<Label Text="{Binding FromTimeString}" HorizontalTextAlignment="Start" FontSize="14" Grid.Column="0" />
<Label Text="{Binding ToTimeString}" HorizontalTextAlignment="Start" FontSize="14" Grid.Column="1" />
<Label Text="{Binding Remarks}" HorizontalTextAlignment="Start" FontSize="14" Grid.Column="2" />

</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>

这是我的 C#

var timings = new List<Timing>();

var timing = new Timing();
timing.Id = 1;
timing.FromTime = DateTime.Now;
timing.FromTimeString = DateTime.Now.ToString("hh:mm ttBel");
timing.ToTime = DateTime.Now;
timing.ToTimeString = DateTime.Now.ToString("hh:mm tt");
timing.Code = "I";
timing.Remarks = "Inside";
timings.Add(timing);
listAttendanceTiming.ItemsSource = timings;

public class Timing
{
public int Id { get; set; }
public DateTime FromTime { get; set; }
public string FromTimeString { get; set; }
public DateTime ToTime { get; set; }
public string ToTimeString { get; set; }
public string Code { get; set; }
public string Remarks { get; set; }
}

异常(exception)

Unhandled Exception:

System.ArgumentException: Value was an invalid value for HeaderTemplateParameter name: value occurred

最佳答案

ListView.HeaderTemplate.DataTemplate 不需要 ViewCell 属性。像下面这样更改 ListView.HeaderTemplate 将解决此问题。

<ListView.HeaderTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<Label Text="From Time" HorizontalTextAlignment="Start" FontSize="14"
Grid.Row="0" Grid.Column="0" />

<Label Text="To Time" HorizontalTextAlignment="Start" FontSize="14"
Grid.Row="0" Grid.Column="1" />

<Label Text="Remarks" HorizontalTextAlignment="Start" FontSize="14"
Grid.Row="0" Grid.Column="2" />

</Grid>
</DataTemplate>
</ListView.HeaderTemplate>

关于listview - Xamarin Forms ListView - 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55851473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com