gpt4 book ai didi

c# - Xamarin.Forms 绑定(bind)指定的转换无效

转载 作者:太空狗 更新时间:2023-10-29 17:40:02 27 4
gpt4 key购买 nike

我有一个奇怪的异常,编译器告诉我指定的转换无效,即使我所做的非常简单。

我有一个绑定(bind)到 ObservableCollection 的 ListView。在我的 Listview 中是一个带有网格的 ViewCell。 Xamarin.Forms 版本 2.3.2.127

<ListView ItemsSource="{Binding GiftCollection}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>

<Label Grid.Row="0" Grid.Column="0" Text="{Binding GiftName}"/>
<Label Grid.Row="1" Grid.Column="0" Text="{Binding GiftDescription}"/>
<Image Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" Source="{Binding GiftImage}"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

型号:

 public class GiftModel {

public string GiftName { get; set; }
public string GiftDescription { get; set; }
public ImageSource GiftImage { get; set; }
}

View 模型:

public class NextRoundViewModel : BaseViewModel {

public NextRoundViewModel(ApplicationModel applicationModel) {
ApplicationModel = applicationModel;
Initialize();
}

public ApplicationModel ApplicationModel { get; set; }
public ObservableCollection<GiftModel> GiftCollection { get; set; }
public string CurrentRound => "Runde 2";

private void Initialize() {
GiftCollection = new ObservableCollection<GiftModel> {
new GiftModel {
GiftName = "100 Punkte",
GiftDescription = "Test",
GiftImage = ImageSource.FromFile("Star.png"),
},
new GiftModel {
GiftName = "200 Punkte",
GiftDescription = "Test",
GiftImage = ImageSource.FromFile("Star.png"),
},
new GiftModel {
GiftName = "300 Punkte",
GiftDescription = "Test",
GiftImage = ImageSource.FromFile("Star.png"),
},
};
}
}

所以我尝试了所有方法,但如果我使用例如 TextCell,异常就消失了。System.InvalidCastException:指定的转换无效。这很奇怪,因为我不知道去哪里寻找 Bug。

最佳答案

我也有这个问题,问题出在xaml .我有一个 <StackLayout>在我的里面 <DataTemplate> , 你可以删除你的 <Grid>这应该可以解决问题。

您知道吗,您可以替换 <Grid><ImageCell> :

<ListView.ItemTemplate>
<DataTemplate>
<ImageCell
Text="{Binding GiftName}"
Detail="{Binding GiftDescription}"
ImageSource="{Binding GiftImage}">
</ImageCell>
</DataTemplate>
</ListView.ItemTemplate>

关于c# - Xamarin.Forms 绑定(bind)指定的转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40575870/

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