Xamarin形式:IValueConverter在Convert方法中接收空值 [英] Xamarin form: IValueConverter receive null value in Convert method
问题描述
我有一个列表视图,其中的商品来源设置为子商品.我想将一个子对象绑定到一个视图,该视图将通过转换器设置颜色.
调用了converter方法,但是我传入的值为 null
.
除点外,我还使用 Path =/
,但传递给转换器的值仍然为 null
.如果我绑定该属性,那很好,但不是当前项.
< ListView x:Name ="childListView"VerticalOptions ="FillAndExpand"HasUnevenRows ="true"ItemSelected ="OnItemSelected"ItemTapped ="OnItemTapped">< ListView.ItemTemplate>< DataTemplate>< ViewCell>< ViewCell.View>< StackLayoutBackgroundColor ="{Binding.,Converter = {StaticResource accountedToColorConverter}}"Spacing ="0" Padding ="0" VerticalOptions ="FillAndExpand" HorizontalOptions ="FillAndExpand">< StackLayout Orientation ="Horizontal" Spacing ="10" Padding ="0" HorizontalOptions ="FillAndExpand" VerticalOptions ="FillAndExpand">< StackLayout Orientation ="Horizontal" HorizontalOptions ="StartAndExpand">< controls:CircleImage>
Phatye肯定是正确的
BackgroundColor ="{Binding.,Converter = {StaticResource accountedToColorConverter}}"
{Binding.}
和 {Binding Path =.}
与转换器一起使用,只是遇到了与您相同的null引用问题碰到.似乎Xamarin不喜欢这样.正确的解决方案是传递要绑定到的属性的正确路径:
假设该属性是顶级属性
BackgroundColor ="{绑定路径=已记帐,Converter = {StaticResource accountedToColorConverter}}"
否则,您可以这样做:
BackgroundColor ="{Binding Path = topLevelProperty.accounted,Converter = {StaticResource accountedToColorConverter}}"
I have a list view set with item source as child. I want to bind a child object to a view which will set the color through the converter.
The converter method got called but the value i passed in was null
.
Apart from dot, I also use Path=/
but the value passed to the converter still null
. If i bind the property, it's fine but not the current item.
<ListView x:Name="childListView"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
ItemSelected="OnItemSelected"
ItemTapped="OnItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout
BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}"
Spacing="0" Padding="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<StackLayout Orientation="Horizontal" Spacing="10" Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand">
<controls:CircleImage>
Phatye is definitely correct in saying the line
BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}"
is the culprit. I too have attempted to use the {Binding .}
and {Binding Path=.}
with a converter in the past only to run into the same null reference issues you're running into. It seems that Xamarin doesn't like this.
The proper solution will be to pass the proper path of the property you want to bind to:
Assuming the property is a top level property
BackgroundColor="{Binding Path=accounted, Converter={StaticResource accountedToColorConverter}}"
Otherwise you could do this:
BackgroundColor="{Binding Path=topLevelProperty.accounted, Converter={StaticResource accountedToColorConverter}}"
这篇关于Xamarin形式:IValueConverter在Convert方法中接收空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!