3
私は、何がやっていることが非常に単純であっても、指定されたキャストは有効ではないことをコンパイラが教えてくれるという奇妙な例外があります。Xamarin.Forms Binding指定されたキャストが無効です
私はObservableCollectionにバインドされた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; }
}
のViewModel:
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:指定されたキャストが無効です。私はバグを探す場所が分からないのでちょっと変です。
特異的結合が問題を引き起こしていますか?バインドされているプロパティのタイプは何ですか? – Jason
イメージが作業中です。ラベルは単純な文字列にバインドされています。ラベルをTextCellに置き換えた場合、例外はなくなります。私はラベルを使用する必要があります:) –
あなたはCategoryNameとDescriptionにバインドしています、あなたのモデルのものですか? – Jason