Xaml Xamarin表单:IValueConverter在Convert方法中接收空值

Xaml Xamarin表单:IValueConverter在Convert方法中接收空值,xaml,xamarin,binding,converter,Xaml,Xamarin,Binding,Converter,我有一个列表视图集,其项源为子项。我想将一个子对象绑定到一个视图,该视图将通过转换器设置颜色 已调用converter方法,但我传入的值为null 除了点,我还使用了Path=/,但是传递给转换器的值仍然是null。如果我绑定属性,则可以,但不是当前项 <ListView x:Name="childListView" VerticalOptions="FillAndExpand" HasUnevenRows="true" ItemSelected="OnIte

我有一个列表视图集,其项源为子项。我想将一个子对象绑定到一个视图,该视图将通过转换器设置颜色

已调用converter方法,但我传入的值为
null

除了点,我还使用了
Path=/
,但是传递给转换器的值仍然是
null
。如果我绑定属性,则可以,但不是当前项

<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>

这条线很可能是罪魁祸首。只有当页面的绑定上下文是单个“AccountedTo”属性时,它才有效。将其更改为
“{Binding BackgroundProperty}”

其中“BackgroundProperty”是“AccountedTo”值。

Phatye说这行绝对正确

BackgroundColor=“{Binding.,Converter={StaticResource accountedToColorConverter}}”

是罪魁祸首。我过去也曾尝试将
{Binding.}
{Binding Path=.}
与转换器一起使用,结果却遇到了与您遇到的相同的空引用问题。似乎Xamarin不喜欢这样

正确的解决方案是传递要绑定到的属性的正确路径:

假设该属性是顶级属性

BackgroundColor=“{Binding Path=accounted,Converter={StaticResource accountedToColorConverter}}”

否则,您可以这样做:


BackgroundColor=“{Binding Path=topLevelProperty.accounted,Converter={StaticResource accountedToColorConverter}}”
这是一个有趣的行为。我最近在使用
CarouselView(Forms.Plugin)
时遇到了这个问题,并且做了更多的研究,结果发现
CarouselView
元素的
BindingContext
由于某种原因被多次设置


因此,第一次,转换器得到
null
值,但最终,它用正确的值被第二次调用,所以我将转换器更改为优雅地处理
null
值,它工作了。

BackgroundColor
属性的绑定中,“.”在代码中意味着什么?如果显示此XAML背后的代码,可能更容易在XAML中找到outIn,值转换器通常在
ResourceDictionary
中实例化,然后使用
StaticResource
标记扩展在绑定表达式中引用。plz提供相关代码以获得帮助。我使用的“.”用于我的其他代码中,我在列表中有一个组绑定,在该组中,我有字符串和其他对象组。“.”表示列表中的当前项。我在模板选择器中使用了它,而不是在IValueConverter上使用它。
BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}"